'How can I fix this syntax error? Non-nullable instance field 'students' must be initialized [duplicate]
The errors are given at StudentAdd(List.. and _StudentAddState(Lİst.. in first and second 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(),
],
),
),
),
);
}
Solution 1:[1]
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(),
],
),
),
),
);
}
Solution 2:[2]
Welcome to Stack overflow, I also experienced this when I started working with Dart Null Safety.
The error is coming out because null safety is enabled (Which is good thing). Dart/Flutter only wants you to ensure that the variable "students" is not 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.
To understand Dart Null safety better, I suggest going through the code lab provided by Google Null Safety Codelab
Happy Fluttering!!!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Josteve |
| Solution 2 | Lone Wolf |
