In the new version of Flutter, I encountered an error as follows.
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)
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;
}
CodePudding user response:
Check the parameters in your state constructor it should be
_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
class _StudentAddState extends State<StudentAdd> with StudentValidationMixin {
List<Student>? get students = widget.students;
var student = Student.withoutInfo();
var formKey = GlobalKey<FormState>();
...
}
CodePudding user response:
If you check this tudentAdd(List<Student>? students)
you are calling StudentAdd
constructor inside _StudentAddState
.
_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
. And to initiate item We have initState
inside state
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);
}
//....
}