I have a common response structure from http request like this
class CommonResponse<T> {
int total;
List<T> data;
bool success;
CommonResponse({this.total, this.data, this.success});
CommonResponse.fromJson(Map<String, dynamic> json) {
total = json['total'];
data = json['data'];
success = json['success'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['total'] = this.total;
data['data'] = this.data;
data['success'] = this.success;
}
}
Now I need to parse the generic data. For example I am getting list of zones inside data object in CommonResponse
class Zone {
bool active;
String id;
String name;
Zone({this.active, this.id, this.name});
Zone.fromJson(Map<String, dynamic> json) {
this.active = json['active'];
this.id = json['id'];
this.name = json['name'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['active'] = this.active;
data['id'] = this.id;
data['name'] = this.name;
}
}
How to get list of zones?
var response = {
"data": [
{
"active": true,
"created_on": "2020-03-12T09:56:11+00:00",
"description": "For Testing",
"id": "b17627eb-8a39-4230-80fe-ebed77f7e0e2",
"name": "Office",
"updated_on": "2020-03-19T12:26:16+00:00"
}
],
"success": true,
"total": 1
};
CommonResponse<Zone> common = CommonResponse.fromJson(response);
print(common.data);
This is what i tried. I am getting perfect response but am getting error getting list of Zone. Error is cannot convert List<dynamic> to Zone.