[英]Flutter error: The argument type 'Color Function(String)' can't be assigned to the parameter type 'dynamic Function(dynamic)'
[英]Flutter(dart) error: The argument type can't be assigned to the parameter type 'dynamic Function(List<Student>?)'
在Flutter的新版本中遇到错误如下。
错误:参数类型“列表?” 无法分配给参数类型“动态函数(列表?)”。 (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;
}
如果你检查这个tudentAdd(List<Student>? students)
你正在调用_StudentAddState
中的StudentAdd
构造函数。
_StudentAddState(StudentAdd(List<Student>? students)) {
this.students = students;
}
你需要像
_StudentAddState(List<Student>? students) {
this.students = students;
}
您也可以避免传递参数,而我们可以使用widget.varableName
访问 class 级变量。 并启动项目我们在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);
}
//....
}
检查 state 构造函数中的参数应该是
_StudentAddState(List<Student>? students)
而且您不需要将数据从 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.