json_serializable.dart icon indicating copy to clipboard operation
json_serializable.dart copied to clipboard

Unnecessary use of a null check ('!') on enum values.

Open isoos opened this issue 5 months ago • 1 comments

Latest version (6.10.0 on Dart 3.8.0) generates the following code for an enum field

Map<String, dynamic> _$XyzToJson(Xyz instance) =>
    <String, dynamic>{
      'state': _$AbcEnumMap[instance.state]!,
    };

The generated _$AbcEnumMap is a const map with all the enums listed, and the linter now complains that it is an unnecessary operation.

isoos avatar Aug 09 '25 14:08 isoos

Ooo! I wonder if it "knows" that the map is complete? That'd be a cool feature...since it's const?

kevmoo avatar Aug 09 '25 19:08 kevmoo