【问题标题】:Flutter: List<dynamic> is not a subtype of type Map<String, dynamic>Flutter:List<dynamic> 不是 Map<String, dynamic> 类型的子类型
【发布时间】:2021-04-11 05:37:50
【问题描述】:

我有关注Model

class ModelCompanies {
  int id;
  String companyid;
  String companyName;
  String name;
  String activeStatus;

  ModelCompanies(
      {this.id, this.companyid, this.companyName, this.name, this.activeStatus});

  ModelCompanies.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    companyid = json['companyid'];
    companyName = json['companyName'];
    name = json['name'];
    activeStatus = json['activeStatus'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['companyid'] = this.companyid;
    data['companyName'] = this.companyName;
    data['name'] = this.name;
    data['activeStatus'] = this.activeStatus;
    return data;
  }
}

我有以下方法

Future<ModelCompanies> fetchCompanies() async {
    //encode Map to JSON

    final response = await _dio.get(
        globals.apiUrl + "get-companies",
        options: buildCacheOptions(Duration(hours: _cachePriorityDurationHours),
            forceRefresh: true,
            maxStale: Duration(days: _networkPriorityDurationDays)));

    if (response.statusCode == 200) {
      return ModelCompanies.fromJson(response.data);
    } else {
      throw new Exception("Failed to load post.");
    }
  }

来自 API 的 JSON 格式。

[
   {
      "id":123,
      "companyid":"XYZ-123",
      "companyName":"XYZ Corporation",
      "name":"(XYZ) XYZ Corporation",
      "activeStatus":"A"
   },
   {
      "id":248,
      "companyid":"PQR-248",
      "companyName":"PQR Holdings Limited",
      "name":"(PQR) PQR Holdings Limited",
      "activeStatus":"A"
   },
   {
      "id":397,
      "companyid":"MNO-397",
      "companyName":"MNO Adventures",
      "name":"(MNO) Adventures",
      "activeStatus":"A"
   }
]

问题 方法fetchCompanies 中的return ModelCompanies.fromJson(response.data); 行给出错误 List&lt;dynamic&gt; is not a subtype of type Map&lt;String, dynamic&gt;

我在 SO 中寻找类似问题的其他解决方案,但无法使它们起作用(因为我对颤振和飞镖相当陌生)。

我尝试的是

 Map<String, dynamic> jsonResponse = response.data.map((item) {
        return {
          "id": item["id"],
          "companyid": item["companyid"],
          "companyName": item["companyName"],
          "name": item["name"],
          "activeStatus": item["activeStatus"]
        };
      }).toList(); 

      return ModelCompanies.fromJson(jsonResponse );

但这也会引发同样的错误。

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    response.data 包含一个JSON String,其中包含一个List 的对象,因此我们应该替换以下行:

    if (response.statusCode == 200) {
     return ModelCompanies.fromJson(response.data);
    }
    

    收件人:

    if (response.statusCode == 200) {
     List<ModelCompanies> modelCompanies = 
        List<ModelCompanies>.from(
           response.data.map((model)=> ModelCompanies.fromJson(model)
          ) 
        );
    }
    

    【讨论】:

    • 谢谢,我现在在Iterable l... 上遇到错误type 'List&lt;dynamic&gt;' is not a subtype of type 'String') 另外请注意我的方法fetchCompanies 应该返回Future&lt;ModelCompanies&gt;
    • 乐于助人!我已编辑代码以解决问题,如果有帮助请告诉我
    • 再次感谢,我应该如何处理modelCompanies ,方法fetchCompanies 的返回类型为FuturemodelCompanies List&lt;ModelCompanies&gt;
    猜你喜欢
    • 2018-12-14
    • 2019-04-03
    • 1970-01-01
    • 2021-10-25
    • 2020-07-03
    • 2019-12-05
    • 2023-03-11
    • 1970-01-01
    • 2021-09-12
    相关资源
    最近更新 更多