I have array of country json objects like this
[{"id":4,"name":"Afghanistan","alpha2":"af","alpha3":"afg"},
{"id":8,"name":"Albania","alpha2":"al","alpha3":"alb"},
{"id":12,"name":"Algeria","alpha2":"dz","alpha3":"dza"},..
I have got them to a map like this
Map<String, dynamic> jsonMap;
jsonMap = json.decode(jsonString);
After this I have the data like this
[{id: 4, name: Afghanistan, alpha2: af, alpha3: afg}, {id: 8, name: Albania, alpha2: al, alpha3: alb}, ...
What i want to do is create a List of county objects from this
Country
{
int id;
String name;
String alpha2;
String alpha3;
}
Can anyone explain how to do this conversion ?
I use this straight forward way:
List<TestModel> myTestModel;
var response = await http.get("myTestUrl");
myTestModel=(json.decode(response.body) as List).map((i) =>
TestModel.fromJson(i)).toList();
The link can be really helpful: json_serializable
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments