ios Flutter(dart)误差:参数类型不能分配给参数类型“dynamic Function(List< Student>?)”

wqnecbli  于 2023-02-10  发布在  iOS
关注(0)|答案(2)|浏览(142)

在Flutter的新版本中,我遇到了如下错误。
错误:无法将参数类型"列表?"赋给参数类型"动态函数(列表?)"。([temel_widget] lib\screens\student_add. dart中的参数类型不可赋值:14)

class StudentAdd extends StatefulWidget {
    //Student addStudent = Student.withId(0, "", "", 0);
    List<Student>? students;
    StudentAdd(List<Student>? students) {
    this.students = students;
  }
  @override
  State<StatefulWidget> createState() {
    return _StudentAddState(students);  **This here error message**
  }
}
class _StudentAddState extends State with StudentValidationMixin {
  //Student addStudent = Student.withId(0, "", "", 0);

  List<Student>? students=[];
  var student = Student.withoutInfo();
  var formKey = GlobalKey<FormState>();

  _StudentAddState(StudentAdd(List<Student>? students)) {
    this.students = students;
  }
h6my8fg2

h6my8fg21#

检查状态构造函数中的参数

_StudentAddState(List<Student>? students)

您不需要将数据从Widget传递到其状态,您可以使用widegt.data从State类访问Widget数据

class _StudentAddState extends State<StudentAdd> with StudentValidationMixin {

  List<Student>? get students = widget.students;
  var student = Student.withoutInfo();
  var formKey = GlobalKey<FormState>();

  ...
}
qvtsj1bj

qvtsj1bj2#

如果选中此tudentAdd(List<Student>? students),则在_StudentAddState中调用StudentAdd构造函数。

_StudentAddState(StudentAdd(List<Student>? students)) {
    this.students = students;
  }

你需要使用

_StudentAddState(List<Student>? students) {
    this.students = students;
  }

你也可以避免传递参数,而我们可以使用widget.varableName来访问类级变量。为了初始化item,我们在state中使用了initState

class StudentAdd extends StatefulWidget {
  StudentAdd({
    Key? key,
    required this.students,
  }) : super(key: key);

  List<Student>? students;
  @override
  State<StudentAdd> createState() => _StudentAddState();
}

class _StudentAddState extends State<StudentAdd> {
  @override
  void initState() {
    super.initState();

    ///getting students also, this can be done anyplace on state class
    print(widget.students?.length);
  }
 //....
}

相关问题