duplicate_map_key
Reports a map literal key that repeats an earlier key of the same literal. Write the key you meant, or delete the entry you do not want.
- Tier
- blocking
- Scope
- Dart
- Lowest preset
- recommended
- Fix
- none
What it reports
Section titled “What it reports”A key that repeats an earlier key of the same map literal: {id: first, id: second}. The violation is on the repeated key, and each further repetition is a violation of its own.
Two keys are the same when they hold the same tokens in the same order and every name means the same declaration, as no_equal_branches compares branches. Whitespace and comments do not count. Another literal, a pair of parentheses or another quote makes them differ.
Only a key that gives the same value each time is compared, such as a local variable, a parameter or a final field.
Which values count as the same twice
- a literal, or a constant: a
constcollection or object, a value of an enum; - a type,
this, a local variable or a parameter; - a field or a top-level variable that is
finalorconst; - the core members that only read their object:
length,isEmpty,isNotEmpty,first,last,isNaN,isNegative,isFinite,isInfinite,isEven,isOdd,sign,hashCodeandruntimeType; - what is built from these with
.and?., the core library’s operators,is,as,!, a conditional expression andidentical.
It does not report:
- a constant key, such as a literal, a
constname or an enum value. The analyzer reports two equal constant keys itself, asequal_keys_in_map. - a key under an
ifor aforof the literal, or one that a spread brings: it may not be there at all. - a key that is a getter written in the source, an index operator or a call,
{ids.next: a, ids.next: b}: it can give another key each time. This holds even for a getter that only reads a field.
A map literal holds one entry per key, with the last value. The first value is evaluated and thrown away, so what it stood for is missing from the map. It happens when you copy an entry and forget to change its key: {home: homeScore, home: awayScore}. Nothing fails at run time; the map is only smaller than it looks.
The rule is blocking because a value is lost without a trace.
Example
Section titled “Example”Map<String, int> scores(String home, String away, int a, int b) => { home: a, home: b,};Map<String, int> scores(String home, String away, int a, int b) => { home: a, away: b,};There is no fix: deleting either entry is a choice between two values, and only you know which key was meant.
Rule parameters
Section titled “Rule parameters”The rule has only the two common rule parameters, set under the assay: key of the project’s root analysis_options.yaml.
| Rule parameter | Default | Effect |
|---|---|---|
enabled |
true |
false switches the rule off for the whole project. |
exclude |
[] |
Path globs where the rule is silent. |
assay: rules: duplicate_map_key: exclude: ["lib/generated/**"]Suppressing it
Section titled “Suppressing it”Write why on the line directly above the suppression, which goes above the repeated key:
final headers = { name: 'first', // The second value replaces the first on purpose, as the protocol says. // ignore: duplicate_map_key name: 'second',};// ignore_for_file: assay/duplicate_map_key silences it in a whole file, and // ignore: assay/duplicate_map_key with the prefix works too. See Suppressions.