I'm not able to convert server response to model class. The following is my code.
void main() {
//JSON to parse
var strJson = """{
\"person\": [
{\"name\": \"Mahendra\", \"age\": 28},
{\"name\": \"Test\", \"age\": 25}
]
}""";
var data = json.decode(strJson);
print("json: $data");
var result = PersonResponse<Person>.fromJSON(data);
print("result: ${result.persons}");
}
Model Class
class Person {
String name;
int age;
Person.fromJSON(Map json) {
this.name = json["name"];
this.age = json["age"];
}
}
class PersonResponse<T> {
List<T> persons;
PersonResponse.fromJSON(Map json) {
this.persons = json["person"];
}
}
When I run this code I'm not able to convert server response to model class. Getting following error...
Unhandled Exception: type
List<dynamic>
is not a subtype of typeList<Person>
Whats wrong with my code. Any suggestions?