I get a json response in sample format like below
// call 1 : /all-users
{
"success": true,
"message": "All Users list",
"data": [{
"code": "USR001",
"name": "User One"
}]
}
// call 2 - /user-details/USR001
{
"success": true,
"message": "User Details of USR001",
"data": {
"code": "USR001",
"firstName": "User",
"lastName": "One",
"email": "userOne@email.com",
}
}
And my ApiResponse model is
class ApiResponse {
bool success;
String message;
List<Data> data;
ApiResponse({this.success, this.message, this.data});
ApiResponse.fromJson(Map<String, dynamic> json) {
success = json['success'];
message = json['message'];
if (json['data'] != null) {
data = new List<Data>();
json['data'].forEach((v) {
data.add(new Data.fromJson(v));
});
}
}
}
The data in the response may be an object or array depends on the call. I would like to reuse the ApiResponse model for both calls by extending the data as separate models. But i don't know how to do it.