【发布时间】: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"}]
我尝试添加到列表语句,但它也不起作用!
【问题讨论】:
-
如果你的问题有json响应就好了。
-
@Seakeng说我现在做了
-
使用像文档flutter.dev/docs/cookbook/networking/fetch-data这样的json来改变
-
查看这个高级 JSON 解析示例stackoverflow.com/a/60224846/3756408