【问题标题】:Flutter错误,参数类型''不能分配给参数类型
【发布时间】:2022-01-23 16:32:42
【问题描述】:

我不断收到这样的错误,有人可以解释错误的含义以及我需要做些什么来防止它们吗?

The argument type 'Future<List<CompanyModel>> Function(String)' can't be assigned to the parameter type 'Future<List<CompanyModel>> Function(String?)?'.

我有一个 ModelClass,我想从 api 获取一些值到可搜索的下拉小部件。

以下是小部件代码

                  DropdownSearch<CompanyModel>(
                    label: "Name",
                    onFind: (String filter) async {
                      var response = await Dio().get(
                        "http://5d85ccfb1e61af001471bf60.mockapi.io/user",
                        queryParameters: {"filter": filter},
                      );
                      var models = CompanyModel.fromJsonList(response.data);
                      return models;
                    },
                  ),

还有我的模特课,很安静。


class CompanyModel {
  final String name;

  CompanyModel({required this.name});

  factory CompanyModel.fromJson(Map<String, dynamic> json) {
    return CompanyModel(name: json['comapny_name']);
  }

  static List<CompanyModel> fromJsonList(List list) {
    return list.map((item) => CompanyModel.fromJson(item)).toList();
  }
  ///this method will prevent the override of toString
  String userAsString() {
    return '#${this.name}';
  }

  ///custom comparing function to check if two users are equal
  bool isEqual(CompanyModel model) {
    return this?.name == model?.name;
  }

  @override
  String toString() => name;


}

【问题讨论】:

    标签: flutter


    【解决方案1】:

    错误表明onFind 属性需要一个带有可空字符串作为参数的函数,您没有传递可空字符串

    尝试更改此设置

    onFind: (String filter) async {
    

    到这里

    onFind: (String? filter) async {
    

    如果这不起作用,您必须添加 DropdownSearch 类的代码,以便我们获取更多信息

    【讨论】:

      猜你喜欢
      • 2021-09-21
      • 2022-10-02
      • 2022-12-03
      • 1970-01-01
      • 2020-02-20
      • 2021-07-05
      • 2020-01-23
      • 2021-12-22
      • 2021-10-17
      相关资源
      最近更新 更多