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';Known limitations
Section titled “Known limitations”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.
Configuration
Section titled “Configuration”This rule is in the opinionated preset, so it is on with
preset: opinionated, or by name:
rules: avoid_missing_enum_constant_in_map: trueTo turn it off again:
rules: avoid_missing_enum_constant_in_map: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”prefer_enums_by_name— Use .byName() instead of .firstWhere() to look up enum values by name.enum_constants_ordering— Keep enum constants in a configured order.avoid_accessing_collections_by_constant_index— Avoid accessing a collection by a constant index inside a loop.avoid_map_keys_contains— Use containsKey() instead of .keys.contains() for better performance.