You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Json_serializable generates a hard-coded enum ↔ string map in the .g.dart file (e.g. _$ExampleTypeEnumMap). This duplicates information that already exists on the enum itself (Enum.values / Enum.name).
This can be error-prone, especially when the enum comes from an external package (ex: pub.dev, monorepo, ...). If that package updates the enum and build_runner is not re-run, the generated map becomes out of sync with the actual enum definition, potentially leading to errors.
Since enums already expose their values and names, it seems possible to rely on them instead of generating and maintaining a separate hard-coded map.
Hard-coded map doesn't work if the enum is defined in another file, since the generated file can not access MyEnum._undefined.
A custom JsonConverter is needed to handle it:
Activity
lucavenir commentedon Jan 7, 2026
Another thing I've noticed is that the following:
won't apply to enums, and indeed this:
Will result in generated files with a map, e.g.
I have to specify
@JsonEnum(fieldRename: .snake)explicitly to make this work. On each and every enum.DinoFive commentedon Jul 2, 2026
Consider a enum that has private enum values:
Hard-coded map doesn't work if the enum is defined in another file, since the generated file can not access
MyEnum._undefined.A custom
JsonConverteris needed to handle it:Rework enum management as @EArminjon said would make this situation simple.