【问题标题】:Flutter - type 'List<dynamic>' is not a subtype of type 'Map<dynamic, dynamic>'Flutter - 类型 'List<dynamic>' 不是类型 'Map<dynamic, dynamic>' 的子类型
【发布时间】:2021-12-02 01:54:05
【问题描述】:

我只是 Flutter 的新手,我认为这是一个新手问题。

我正在尝试获取我在 API 上调用的数据并将其保存到我的模型中,但它的错误类型为“列表”不是“地图”类型的子类型

这是我的模型的副本

class AdTemplate{
  final int id; 
  final String filePath; 
  final String notification; 
  final String status; 
  final int whenAdded; 

  AdTemplate(
      {this.id, 
      this.filePath, 
      this.notification,
      this.status, 
      this.whenAdded});

  factory AdTemplate.fromJson(Map<String, dynamic> json) {
    return  AdTemplate(
      id: json['ID'], 
      filePath: json['FilePath'],
      notification: json['Notification'], 
      status: json['Status'], 
      whenAdded: json['WhenAdded']
    );
  }
}

这是我的功能

Future<AdTemplate> getActiveBannerNotif() async {  
    try { 
      String url = 'https://api/path/'; 
      var res = await http.get(url);  
      final Map data = convert.jsonDecode(res.body);

      if (res.statusCode == 200) { 
        print("Data Fetch!");    
        AdTemplate template = AdTemplate.fromJson(data);  

        return template;
      } else {
        print('No data.'); 

        return null;
      }
    } catch (e) {
      print(e);
      return null;
    } 
}

这是我从 API 获得的示例数据

[{"ID":49,"FilePath":"20210903t171244.png","Notification":"ACT","WhenAdded":1630689165,"Status":"INA"}]

【问题讨论】:

  • 错误在哪一行?
  • @MiguelEscobarCalderon 这一行 AdTemplate template = AdTemplate.fromJson(data);
  • 你能把你的 JSON 字符串添加到问题中吗
  • @rosh-dev 我添加了它。
  • 您正在接收一个数组。导致错误。更改您的 API 或代码

标签: json flutter


【解决方案1】:

你的 api 返回一个列表

[{"ID":49,"FilePath":"20210903t171244.png","Notification":"ACT","WhenAdded":1630689165,"Status":"INA"}]

在转换为模型之前尝试获取这样的数据:data[0]data.first

【讨论】:

    【解决方案2】:

    您从 API 接收到一个数组,这会导致错误。更改如下以访问数组中的单个元素

    Future<AdTemplate> getActiveBannerNotif() async {  
        try { 
          String url = 'https://api/path/'; 
          var res = await http.get(url);  
          
          if (res.statusCode == 200) { 
            print("Data Fetch!");  
            final data = convert.jsonDecode(res.body);
            AdTemplate template = AdTemplate.fromJson(data[0]);  
    
            return template;
          } else {
            print('No data.'); 
    
            return null;
          }
        } catch (e) {
          print(e);
          return null;
        } 
    }
    
    

    【讨论】:

      【解决方案3】:

      API 返回 JSON 数组而不是 json 对象,因此是 List 而不是 Map。

      试试:

      if (res.statusCode == 200) { 
          print("Data Fetch!");    
          AdTemplate template = AdTemplate.fromJson(json.decode(utf8.decode(res.bodyBytes)));  
      
          return template;
        }
      

      【讨论】:

      • 我尝试了你的建议,但它的错误类型相同'List'不是'Map'类型的子类型
      • 显示所有新代码 (getActiveBannerNotif)
      猜你喜欢
      • 1970-01-01
      • 2020-05-19
      • 2021-09-08
      • 2021-08-05
      • 2023-01-07
      • 2019-08-18
      • 2019-04-03
      • 2021-06-09
      • 2021-12-29
      相关资源
      最近更新 更多