[英]How can I fix this syntax error? Non-nullable instance field 'students' must be initialized
The errors are given at StudentAdd(List.. and _StudentAddState(Lİst.. in first and second class错误在 StudentAdd(List.. 和 _StudentAddState(Lİst.. 在第一个和第二个 class
Error is saying to me two times that 'Non-nullable instance field 'students' must be initialized.'错误两次对我说“必须初始化不可为空的实例字段'学生'。” and Non-nullable instance field 'students' must be initialized.
和不可为空的实例字段“学生”必须被初始化。
All my code:我所有的代码:
class StudentAdd extends StatefulWidget {
List<Student> students;
StudentAdd(List<Student> students){
this.students = students;
}
@override
State<StatefulWidget> createState() {
return _StudentAddState(students);
}
}
class _StudentAddState extends State with StudentValidationMixin {
List<Student> students;
var student = Student.withInfo(0, '', '', 0);
var formKey = GlobalKey<FormState>();
_StudentAddState(List<Student> students){
this.students = students;
}
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
appBar: AppBar(
title: Text('Yeni Öğrenci Ekle'),
),
body: Container(
margin: EdgeInsets.all(20.0),
//margin: EdgeInsets.only(top: 20.0, right: 20.0, left: 20.0),
child: Form(
key: formKey,
child: Column(
children: <Widget>[
buildFirstNameField(),
buildLastNameField(),
buildGradeField(),
buildSubmitButton(),
],
),
),
),
);
}
Welcome to Stack overflow, I also experienced this when I started working with Dart Null Safety.欢迎来到 Stack Overflow,当我开始使用 Dart Null Safety 时,我也遇到了这种情况。
The error is coming out because null safety is enabled (Which is good thing).出现错误是因为启用了 null 安全性(这是好事)。 Dart/Flutter only wants you to ensure that the variable "students" is not null.
Dart/Flutter 只希望您确保变量“students”不是 null。
you can do that by declaring your constructor like this你可以通过像这样声明你的构造函数来做到这一点
StudentAdd(this.students);
_StudentAddState(this.students);
Or you if you want the students variable to be nullable you can declare it as thus.或者,如果您希望学生变量可以为空,您可以这样声明它。
List<Student>? students;
The "?"这 ”?” tells dart/flutter that the students variable is nullable.
告诉 dart/flutter 学生变量可以为空。
To understand Dart Null safety better, I suggest going through the code lab provided by Google Null Safety Codelab为了更好地了解 Dart Null 安全性,我建议通过谷歌提供的代码实验室Null Safety Codelab
Happy Fluttering!!!快乐飘飘!!!
Try this:尝试这个:
class StudentAdd extends StatefulWidget {
final List<Student> students;
StudentAdd(this.students);
@override
State<StatefulWidget> createState() => _StudentAddState();
}
class _StudentAddState extends State with StudentValidationMixin {
late List<Student> students;
@override
initState(){
students = widget.students;
setState((){});
}
var student = Student.withInfo(0, '', '', 0);
var formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
appBar: AppBar(
title: Text('Yeni Öğrenci Ekle'),
),
body: Container(
margin: EdgeInsets.all(20.0),
//margin: EdgeInsets.only(top: 20.0, right: 20.0, left: 20.0),
child: Form(
key: formKey,
child: Column(
children: <Widget>[
buildFirstNameField(),
buildLastNameField(),
buildGradeField(),
buildSubmitButton(),
],
),
),
),
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.