Getting errors after trying to convert nested JSON data using dart's factory concept. Here I have two classes to handle the json data, but still getting this error:
Exception has occurred. _TypeError (type 'FormatException' is not a subtype of type 'Map')
Here is the code:
class BodyResponse {
final Map<String, dynamic> message;
BodyResponse({
this.message
});
factory BodyResponse.fromJson(Map<String, dynamic> json) {
return BodyResponse(message: json['message']);
}
}
class ErrorResponse {
final BodyResponse body;
final int status;
final String contentType;
final bool success;
ErrorResponse({
this.body, this.status, this.contentType, this.success
});
factory ErrorResponse.fromJson(Map<String, dynamic> json) {
return ErrorResponse(
body: BodyResponse.fromJson(json['body']),
status: json['status'],
contentType: json['content_type'],
success: json['success']
);
}
}
ErrorResponse errors = ErrorResponse.fromJson("""
{
"body": {
"message": "Some one has already taken this username(fooBar), please try again with a new username."
},
"status": 500,
"content_type": "application\/json",
"success": false
}
""");
print(errors);
What could go wrong here?
ErrorResponse errors = ErrorResponse.fromJson(...)seems to be a string, while the function expects it to be a map (Map<String, dynamic> json).