【问题标题】:How to solve this List<dynamic> type error in Flutter/Dart如何解决 Flutter/Dart 中的 List<dynamic> 类型错误
【发布时间】:2021-04-25 03:22:19
【问题描述】:

我对扑扑和飞镖很陌生。试图从这个未来返回一个从 api 中提取数据的列表,但我一直收到这个错误。这里有人通过转换为列表帮助我解决了类似的错误,因为我在返回可迭代的数组上调用 map 函数,但在这种情况下,我不确定需要做什么。

type List<dynamic> is not a subtype of type FutrueOr<ListCity>>

收到的数据如下:

{
 "data": {
        "127287": {
            "Feature_int_id": "127287",
            "Admin1_str_code": "US06",
            "Country_str_code": "US",
            "Feature_str_name": "Acampo",
            "Feature_dec_lat": "38.194",
            "Feature_dec_lon": "-121.25"
        },
        "116496": {
            "Feature_int_id": "116496",
            "Admin1_str_code": "US06",
            "Country_str_code": "US",
            "Feature_str_name": "Acton",
            "Feature_dec_lat": "34.49",
            "Feature_dec_lon": "-118.22"
        },
        "124284": {
            "Feature_int_id": "124284",
            "Admin1_str_code": "US06",
            "Country_str_code": "US",
            "Feature_str_name": "Adelanto",
            "Feature_dec_lat": "34.665",
            "Feature_dec_lon": "-117.512"
        },
}

下面是未来的代码:

Future<List<City>> fetchCitiesByProvince(provinceCode) async {
  final response = await http.get(Uri.https('localhost/msd', 'api/cities/' + provinceCode));
  final responseJson = json.decode(response.body);
  final dataMap =  responseJson['data'];

  if (response.statusCode == 200) {
 
    List citiesList = [];
    for (var city in dataMap.keys) {
      if (dataMap[city]['Admin1_str_code'] == provinceCode) {
        citiesList.add(
          {
            'cityCode': dataMap[city]['Feature_int_id'],
            'cityName': dataMap[city]['Feature_str_name']
          }
        );
      }
    }
    return citiesList;
  } else {
    throw Exception('Failed to load cities');
  }
}

城市等级:

class City {
  final String cityCode;
  final String cityName;

  City({@required this.cityCode, @required this.cityName});

  factory City.fromJson(Map<String, dynamic> json) {

    return City(
      cityCode: json['Feature_int_id'],
      cityName: json['Feature_str_name']
    );
  }
}

【问题讨论】:

    标签: flutter dart types


    【解决方案1】:

    您需要在您的方法中返回List&lt;City&gt;。因此,更改以下代码:

    List citiesList = [];
    

    List<City> citiesList = [];
    

    --- 更新 ---

    您需要使用您的 City 构造函数或工厂从 json 生成项目,如下所示:

    City.fromJson(dataMap[city]);
    
    // Or
    
    City(cityCode: dataMap[city]['Feature_int_id'],
            cityName: dataMap[city]['Feature_str_name']
        );
    

    这里是更新的示例代码:

    Future<List<City>> fetchCitiesByProvince(provinceCode) async {
      final response = await http.get(Uri.https('localhost/msd', 'api/cities/' + provinceCode));
      final responseJson = json.decode(response.body);
      final dataMap =  responseJson['data'];
    
      List<City> citiesList = [];
    
      if (response.statusCode == 200) {
        
        for (var city in dataMap.keys) {
          if (dataMap[city]['Admin1_str_code'] == provinceCode) {
            citiesList.add(City.fromJson(dataMap[city]));
          }
        }
      } else {
    
        throw Exception('Failed to load cities');
        // either throwing an error or return empty list.
      }
    
      return citiesList;
    }
    

    【讨论】:

    • 感谢回复,我已经试过了,还是不行。运行应用程序时我仍然遇到相同的错误,但在运行它之前,我可以在以红色突出显示的代码中看到一个问题,并将鼠标悬停在它上面说:参数类型'Map '不能分配给参数类型“城市”
    • 在我的问题中为我的 City 类添加了代码
    • @sayayin:你需要使用 City 构造函数或工厂。请参阅我的更新答案。
    • 所以我在想我不应该在城市类中做出任何改变,而只是你在上面发布的那个? cityList.add(City.fromJson(dataMap[city]));有了这个我仍然得到同样的错误
    • 说得太快了,实际上它起作用了。我错过了定义城市列表的更改。它仍然是 List cityList = [];而不是 List 城市列表 = [];现在一切都好,谢谢!
    猜你喜欢
    • 2020-06-06
    • 2021-09-08
    • 2022-11-02
    • 1970-01-01
    • 2021-04-06
    • 1970-01-01
    • 1970-01-01
    • 2022-12-02
    相关资源
    最近更新 更多