简体   繁体   English

如何修复此语法错误? 必须初始化不可为空的实例字段“学生”

[英]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.

相关问题 错误:必须初始化不可为空的实例字段“结果” - error: Non-nullable instance field 'result' must be initialized 错误:必须初始化不可为空的实例字段“searchSnapshot” - error: Non-nullable instance field 'searchSnapshot' must be initialized 错误:必须初始化不可为空的实例字段“screenSize” - error: Non-nullable instance field 'screenSize' must be initialized 不可为空的实例字段必须初始化和未处理的错误 LateInitializationError - Non-nullable instance field must be initialized and Unhandled error LateInitializationError 当我添加日期选择器时,必须初始化显示“不可为空的实例字段”selectedDate“。 ' 错误 - When I add date picker then show 'Non-nullable instance field 'selectedDate' must be initialized. ' error 如何解决 Flutter/Dart 中的“不可为空的实例字段 &#39;_controller&#39; 必须初始化”错误? - How to resolve "Non-nullable instance field '_controller' must be initialized" error in Flutter/Dart? 不可为空的实例字段 &#39;_database&#39; 必须被初始化 - Non-nullable instance field '_database' must be initialized 必须初始化不可为空的实例字段“_selectedSize” - Non-nullable instance field '_selectedSize' must be initialized 必须初始化不可为空的实例字段“id” - Non-nullable instance field 'id' must be initialized 不可为空的实例字段 '_bmi' 必须初始化 flutter - Non-nullable instance field '_bmi' must be initialized flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM