Skip to content

avoid_missing_enum_constant_in_map

v0.8.0 Warning Collection Type

Flags a map literal whose key type is an enum but whose entries do not cover every constant of that enum. The diagnostic names the missing ones.

A map keyed by an enum is almost always meant as a total lookup table — labels, icons, colors, route names. A missing constant makes map[value] return null rather than fail, so the gap travels: it becomes a null-check crash, an empty label, or a ! that throws somewhere unrelated.

Adding an enum constant is where this bites. A switch is checked for exhaustiveness by the compiler, so a new constant produces an error at every site that must change; a map gets no such check, and every lookup table in the codebase goes quietly incomplete.

enum Status { active, inactive, pending }
const statusLabels = <Status, String>{
Status.active: 'Active',
Status.inactive: 'Inactive',
// pending is missing — lookups return null
};
enum Status { active, inactive, pending }
const statusLabels = <Status, String>{
Status.active: 'Active',
Status.inactive: 'Inactive',
Status.pending: 'Pending',
};

When a total map is not what you want, make the fallback explicit at the lookup instead:

final label = statusLabels[status] ?? 'Unknown';

The rule only reports when the map’s contents can be enumerated statically. It stays silent when the literal contains a spread (...other), an if or for element, or a key that is not a plain enum constant reference — in those cases the final key set is not knowable at analysis time.

An empty map literal is also skipped, since that is a deliberate starting point rather than an oversight.

This rule is in the opinionated preset, so it is on with preset: opinionated, or by name:

many_lints.yaml
rules:
avoid_missing_enum_constant_in_map: true

To turn it off again:

many_lints.yaml
rules:
avoid_missing_enum_constant_in_map: false

To keep the rule on but skip certain paths, use per-rule exclude.