Skip to main content

avoid-duplicate-map-keys

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',
// 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.
...{'key': 'value'},
// LINT: Avoid duplicate keys in maps. Try removing or changing the key.
if (...) 'key': 'value',
};

final override = {
if (...) '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.
'key': 'value',
};
}

✅ 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