Skip to main content

avoid-duplicate-map-keys

added in: 1.13.0
🛠
Pro+
preset: recommended

Warns when a map has duplicate keys.

Using duplicate keys in the same map declaration is undesirable, since each such key will override the previous value.

Example

❌ Bad:

void fn() {
final map = {
'key': 'value',
'key': 'value', // LINT: Avoid duplicate keys in maps. Try removing or changing the key.
...{'key': 'value'}, // LINT: Avoid duplicate keys in maps. Try removing or changing the key.
if (...) 'key': 'value', // LINT: Avoid duplicate keys in maps. Try removing or changing the key.
};

final override = {
if (...) 'key': 'value',
'key': 'value', // LINT: This entry unconditionally overrides the previous entry with the same key. Try changing the key or removing the entry inside an if element.
};
}

✅ Good:

void fn() {
final map = { // Correct, no duplicate keys
'key': 'value',
'anotherKey': 'value',
...{'thirdKey': 'anotherValue'},
};

final override = {
'key': 'value', // Correct, no duplicate keys
};
}

Additional Resources