【问题标题】:_TypeError (type '(dynamic) => Null' is not a subtype of type '(String, dynamic) => void' of 'action')_TypeError(类型 \'(dynamic) => Null\' 不是类型 \'(String, dynamic) => void\' 的 \'action\' 的子类型)
【发布时间】:2023-02-24 16:53:34
【问题描述】:

我正在做 GET api,然后专门用于建模,我收到了这个错误,我不知道该怎么做

json 链接:https://api-zingmp3-vercel.vercel.app/api/home

enter image description here

【问题讨论】:

  • 添加有问题的代码 sn-p

标签: flutter


【解决方案1】:

在调用 forEach() 之前,你应该转换 json 项目列表:

(json['items'] as List).forEach()

【讨论】:

    【解决方案2】:
    if (json['items'] != null) {
    
      json['items'].forEach((e) {
        items.add(Item.fromJson(e));
    });
    ...
    }
    

    尝试将上面的代码更改为 A、B 和 C。

    A

      if (json['items'] != null) {
    
         var itemBox = json[“items"] as List<Map<String, dynamic>>;
         itemBox.forEach((e) {
            items.add(Item.fromJson(e));
         });
         ...
      }
    

      if (json['items'] != null) {
    
         var itemBox = json[“items"] as List<Map<String, dynamic>>;
         itemBox.map<Item>((e) => testList.add(Item.fromJson(e)) as Item);
         ...
      }
    

    C

     if (json['items'] != null) {
    
        items = List<Item>.from(
        (json['items'] as List<Map<String, dynamic>>).map((x) => Item.fromJson(x)));
        ...
      }
    

    【讨论】:

      猜你喜欢
      • 2021-09-12
      • 2021-02-25
      • 2020-06-21
      • 2020-11-25
      • 2019-08-18
      • 2021-01-02
      • 2020-01-18
      • 2022-07-22
      • 2019-09-02
      相关资源
      最近更新 更多