【发布时间】:2021-09-08 15:06:01
【问题描述】:
我的 JSON 有点像这样:
{"data":{"id":1,"title":"Title 1", "images": [{"small": "link", "large": "link"}]}}
我的模型课:
class Test {
final int id;
final String title;
final Images images;
Test({required this.id,
required this.title,
required this.images});
Test.fromJson(Map<dynamic, dynamic> parsedJson) :
id = parsedJson["id"],
title = parsedJson["title"],
images = Images.fromJson(parsedJson['images']);
class Images {
final String small;
final String large;
Images({
required this.small,
required this.large
});
factory Images.fromJson(Map<dynamic, dynamic> json) {
return Images(
small : json["small"] as String,
large : json["large"] as String
);}
}
这是我的 api 调用:
static Future<Test> getTest(int id) async{
final response = await http.get(Uri.parse("url_here"));
if(response.statusCode == 200){
Map<String, dynamic> json = jsonDecode(response.body);
dynamic body = json['data'];
Test test = Test.fromJson(body);
return test;
}
else{
throw("message");
}
}
如何在视图类中获取 images.small?如果我需要澄清我的问题,请告诉我。我在尝试获取图像时收到错误列表不是 Map
【问题讨论】:
标签: json list flutter dictionary