[英]Flutter(dart) error: The argument type can't be assigned to the parameter type 'dynamic Function(List<Student>?)'
In the new version of Flutter, I encountered an error as follows.在Flutter的新版本中遇到错误如下。
error: The argument type 'List?'错误:参数类型“列表?” can't be assigned to the parameter type 'dynamic Function(List?)'.
无法分配给参数类型“动态函数(列表?)”。 (argument_type_not_assignable at [temel_widget] lib\screens\student_add.dart:14)
(argument_type_not_assignable 在 [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;
}
If you check this tudentAdd(List<Student>? students)
you are calling StudentAdd
constructor inside _StudentAddState
.如果你检查这个
tudentAdd(List<Student>? students)
你正在调用_StudentAddState
中的StudentAdd
构造函数。
_StudentAddState(StudentAdd(List<Student>? students)) {
this.students = students;
}
you need to use like你需要像
_StudentAddState(List<Student>? students) {
this.students = students;
}
Also you can avoid passing parameters while we can access class level variables using widget.varableName
.您也可以避免传递参数,而我们可以使用
widget.varableName
访问 class 级变量。 And to initiate item We have initState
inside state并启动项目我们在
initState
中有 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);
}
//....
}
Check the parameters in your state constructor it should be检查 state 构造函数中的参数应该是
_StudentAddState(List<Student>? students)
And you don't need to pass data from the Widget to its state, you can access widget data from the State class using widegt.data
而且您不需要将数据从 Widget 传递到其 state,您可以使用
widegt.data
从 State class 访问 widget 数据
class _StudentAddState extends State<StudentAdd> with StudentValidationMixin {
List<Student>? get students = widget.students;
var student = Student.withoutInfo();
var formKey = GlobalKey<FormState>();
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.