json_serializable.dart
json_serializable.dart copied to clipboard
Not parse response when data is empty for a key
Hey, I have a model like this, in this scenario in failure case my data can be empty and there will be an error in response. Something like this. But on Message model it is failing as keys are not empty in that mode. How we can handle this case?
{
"data": {
},
"success": false,
"errors": [
{
}
]
}
@JsonSerializable()
class MessageResponse {
factory MessageResponse.fromJson(Map<String, dynamic> json) =>
_$MessageResponseFromJson(json);
Map<String, dynamic> toJson() => _$MessageResponseToJson(this);
@JsonKey(name: "data")
late Message data;
@JsonKey(name: "errors")
late List<Map<String, dynamic>> errors;
MessageResponse(
this.data,
this.success,
this.errors,
);
}
class Message {
factory Message.fromJson(Map<String, dynamic> json) =>
_$MessageFromJson(json);
Map<String, dynamic> toJson() => _$MessageToJson(this);
@JsonKey(name: "id")
late String id;
}
How about adding a default value?
so, I don't think it will result in a null error.
@Ryotaguchi0803 idea seems good here!