I'm looking for a general solution for handling parsing errors of complex (nested + fields of Any type) objects using Freezed and json_serializable.
It should use the default value in case a field can't be parsed, and not compromise the whole object.
Let's take this class for example:
@freezed
class Interest with _$Interest {
const factory Interest({
@Default('') String id,
required String slug,
required String name,
required String image,
required bool isPublic,
}) = _Interest;
factory Interest.fromJson(Map<String, dynamic> json) =>
_$InterestFromJson(json);
}
For illustration purposes, the backend response that I'm parsing could either have the id
as String
(as I have defined it) or int
.
In the latter case, I will get a parsing error (as my id
is a String
and I received an int
).
As a consequence, the whole object can't be parsed, even though the rest of the data is fine.
Thanks! ✌️