简体   繁体   English

Flutter(dart) 错误:无法将参数类型分配给参数类型 'dynamic Function(List<student> ?)'</student>

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

相关问题 Flutter 错误:无法将参数类型“Color Function(String)”分配给参数类型“dynamic Function(dynamic)” - Flutter error: The argument type 'Color Function(String)' can't be assigned to the parameter type 'dynamic Function(dynamic)' 参数类型“Widget Function(Categoria)”不能分配给参数类型“dynamic Function(Child)”。 (型号) Flutter - The argument type 'Widget Function(Categoria)' can't be assigned to the parameter type 'dynamic Function(Child)'. (Model) Flutter 参数类型'列表<dynamic> ' 不能分配给参数类型 'List<value*> *', flutter 库比蒂诺日期选择器内 form_bloc</value*></dynamic> - The argument type 'List<dynamic>' can't be assigned to the parameter type 'List<Value*>*' , flutter cupertino date picker inside form_bloc 错误:无法将参数类型“字符串”分配给参数类型“Uri”。 - 'Uri' 来自 'dart:core' - Error: The argument type 'String' can't be assigned to the parameter type 'Uri'. - 'Uri' is from 'dart:core' 参数类型'响应<dynamic> ' 不能分配给 Flutter 上的参数类型 'String'</dynamic> - The argument type 'Response<dynamic>' can't be assigned to the parameter type 'String' on Flutter Flutter - 参数类型 &#39;Complexity/*1*/&#39; 不能分配给参数类型 &#39;Complexity/*2*/ - Flutter - argument type 'Complexity/*1*/' can't be assigned to the parameter type 'Complexity/*2*/ 参数类型“String”不能分配给参数类型“Uri”。 Flutter - The argument type 'String' can't be assigned to the parameter type 'Uri'. Flutter 参数类型 SearchBar 不能分配给 flutter 中的参数类型 Widget - The argument type SearchBar can't be assigned to the parameter type Widget in flutter Flutter - 在自动完成搜索位置 - 参数类型 &#39;List<String> ?&#39; 不能分配给参数类型“列表”<String> &#39; - Flutter - In autocomplete search location - The argument type 'List<String>?' can't be assigned to the parameter type 'List<String>' 参数类型 'List<dynamic> ' 不能分配给参数类型 'List<widget> '</widget></dynamic> - The argument type 'List<dynamic>' can't be assigned to the parameter type 'List<Widget>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM