【问题标题】:Flutter Error : [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>'Flutter 错误:[错误:flutter/lib/ui/ui_dart_state.cc(157)] 未处理的异常:类型 'List<dynamic>' 不是类型 'Map<String, dynamic>' 的子类型
【发布时间】:2020-01-17 02:17:27
【问题描述】:

我想从 api 获取列表视图并将其放入我的应用程序屏幕,但它告诉我

[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>'

在我的提供者中,我添加了我需要的未来,但它现在有一个错误,我无法运行登录应用程序

List<Country> _country;
List<Country> get country => _country;

Future<dynamic> countryList() async {
    _isLoading = true;
    notifyListeners();

      http.Response  response = await http.get(Environment.country,
        headers: Environment.requestHeader);

    Map<String,dynamic> res = json.decode(response.body);
    var results;
    if (res['code'] == 200) {
      _country = [];
      res['message'].forEach((v) {
        _country.add(new Country.fromJson(v));
      });
      results = true;
    } else {
      results =
          FailedRequest(code: 400, message: res['message'], status: false);
    }
    _isLoading = false;
    notifyListeners();
    return results;
  }

这是我用来返回列表的国家分类模型

class Country{
  String name;
  String photo;

  Country({
    this.name,
    this.photo,
  });

  Country.fromJson(Map<String, dynamic> json){
    name = json['countryName'];
    photo = json['countryImage'].toString();
  }
}

我的 json 响应

respond data : [{"countryName":"Egypt","countryImage":"https://i.ytimg.com/vi/TWaReNkjTLk/maxresdefault.jpg"},{"countryName":"Canada","countryImage":"https://pix10.agoda.net/geo/country/100/3_100_canada_02.jpg?s=1920x"},{"countryName":"US","countryImage":"https://www.green-card.com/assets/Uploads/living-usa-states-florida-green-card.jpg"}]

我尝试添加到列表语句,但它也不起作用!

【问题讨论】:

标签: listview flutter


【解决方案1】:

我想问题出在这里:

Map&lt;String,dynamic&gt; res = json.decode(response.body);

你的 json 返回了一个地图列表:

respond data : [{"countryName":"Egypt","countryImage":"https://i.ytimg.com/vi/TWaReNkjTLk/maxresdefault.jpg"},{"countryName":"Canada","countryImage":"https://pix10.agoda.net/geo/country/100/3_100_canada_02.jpg?s=1920x"},{"countryName":"US","countryImage":"https://www.green-card.com/assets/Uploads/living-usa-states-florida-green-card.jpg"}]

但是,您正在将作为列表的响应分配给地图。

尝试将Map&lt;String,dynamic&gt; res = json.decode(response.body); 更改为List res = json.decode(response.body);

希望对你有帮助!

【讨论】:

    【解决方案2】:

    您的响应数据返回地图列表!

    如果您确定只有一张地图(国家或其他),请更改您的

    Map<String,dynamic> res = json.decode(response.body);
    

    Map<String,dynamic> res = json.decode(response.body)[0];
    

    如果有多个地图(国家或其他),则更改您的

    Map<String,dynamic> res = json.decode(response.body);
    

    List<Map<String,dynamic>> res = json.decode(response.body);
    

    【讨论】:

      猜你喜欢
      • 2020-05-09
      • 2021-04-06
      • 2021-09-08
      • 2021-08-03
      • 2022-11-02
      • 2021-12-29
      • 2021-05-10
      • 1970-01-01
      相关资源
      最近更新 更多