avoid_missing_enum_constant_in_map
v0.8.0 Warning Collection Type
This rule flags a map literal whose key type is an enum but whose entries do not cover every constant of that enum.
Why use this rule
Section titled “Why use this rule”A map keyed by an enum is almost always intended as a total lookup table — labels, icons, colors, route names. When a constant is missing, map[value] returns null rather than failing, so the gap travels: it becomes a null-check crash, an empty label, or a ! that throws somewhere unrelated.
The bigger risk is drift. A switch over an enum is checked for exhaustiveness by the compiler, so adding a constant produces errors at every site that must change. A map gets no such check — add a constant and every lookup table in the codebase is quietly incomplete.
See also: Dart enums
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.