Skip to content

Use $enumEncode for non-nullable enum toJson lookups - #1587

Open
AzazelSensei wants to merge 1 commit into
google:masterfrom
AzazelSensei:fix/unnecessary-enum-bang-1515
Open

Use $enumEncode for non-nullable enum toJson lookups#1587
AzazelSensei wants to merge 1 commit into
google:masterfrom
AzazelSensei:fix/unnecessary-enum-bang-1515

Conversation

@AzazelSensei

Copy link
Copy Markdown

Summary

Generated toJson for non-nullable enums currently emits _$EnumMap[instance.field]!. When that expression is stored in a Map<String, dynamic>, analyzers that enable unnecessary_null_checks flag the bang as unnecessary.

This adds a small $enumEncode helper (alongside $enumDecode) and uses it for non-nullable enum encoding so generated call sites stay lint-clean while still producing a non-nullable value for Map keys and other typed contexts (#1145).

Fixes #1515

Test plan

Avoid generating `_$EnumMap[value]!`, which triggers
unnecessary_null_checks when the result is stored in a
Map<String, dynamic>, while still producing a non-nullable
value for Map keys and other typed contexts.

Fixes google#1515
@google-cla

google-cla Bot commented Aug 12, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant