3

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(),
            ],
          ),
        ),
      ),
    );
  }
sfkkskn
  • 31
  • 4

2 Answers2

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(),
            ],
          ),
        ),
      ),
    );
  }
Josteve
  • 11,459
  • 1
  • 23
  • 35
1

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!!!

Lone Wolf
  • 404
  • 3
  • 11
  • List? students; that is wondeful, ı didnt know that. thank you so much, ı will take a look, @Null Safety Codelab. – sfkkskn Jan 25 '22 at 22:36