You can't really avoid using Map<string, dynamic>.
Because that's the only way you map a JSON into a class object, but if you prefer to Convert a JSON into a T class here is the example.
first, create ServiceResult as you do
Then create T class for the result
class ServiceResult
{
T result;
String message;
bool hasError;
bool hasSuccessMessage;
}
class T{
String yourStringHere;
String yourAnotherStringHere;
int yourIntHere;
}
crucial part
ServiceResult.fromJson(Map<String, dynamic> json) {
message= json['message'];
hasError= json['has_error'];
hasSuccessMessage= json['has_success_message'];
result = json['result'] != null ? new result .fromJson(json['result']) : null;
}
T.fromJson(Map<String, dynamic> json) {
yourStringHere= json['your_string_here'];
yourAnotherStringHere= json['your_another_string_here'];
yourIntHere= json['your_int_here'];
}
bonus: Javier Lecuona create genius tools to convert a JSON into a class object, so you can be lazy like I do :P
here: https://javiercbk.github.io/json_to_dart/
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…