I have a freezed class
@freezed
class Lobby with _$Lobby implements DataObject {
@JsonSerializable(fieldRename: FieldRename.snake, explicitToJson: true)
const factory Lobby({
Redirect? redirect,
}) = _Lobby;
const Lobby._();
factory Lobby.fromJson(Map<String, dynamic> json) => _$LobbyFromJson(json);
}
and an enum
enum Redirect {
test1('/thisistest1'),
test2('/thisistest2'),
const Redirect(this.path);
factory Redirect.fromPath(String path) {
return values.firstWhere((e) => e.path == path);
}
final String path;
}
When constructing my Lobby-Class I use the fromJson factory. In my Json I have the path and not the Redirect enum so I need to translate that.
Can I either tell freezed to use the Redirect.fromPath factory or can I somehow change the .fromJson accordingly?
What would be the best approach?
EDIT I have this data json in my edge-case, this is the print
{redirect: null}
And I am calling Lobby.fromJson(data)
You could try JsonConverter:
More info freezed