My JSON is somewhat like this:
{"data":{"id":1,"title":"Title 1", "images": [{"small": "link", "large": "link"}]}}
My model class:
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
);}
}
Here is my api call:
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");
}
}
How do I get images.small in view class? Please let me know if I need to clear my question. I'm getting an error list is not a subtype of type Map<dynamic, dynamic> while trying to fetch images but I'm not able to covert map to list.