avoid-duplicate-collection-elements
Warns when a collection has duplicate collection elements.
Duplicate collection elements are usually the result of a typo and a sign of a bug.
⚙️ Config
Set ignore-literals
(default is false
) to exclude literals (example).
analysis_options.yaml
dart_code_metrics:
rules:
- avoid-duplicate-collection-elements:
ignore-literals: false
Example
❌ Bad:
void fn() {
final list = <String>[...];
final anotherList = [
...list,
...list, // LINT: Avoid duplicate collection elements. Try removing the duplicate element or replacing it with a different element.
if (list.isNotEmpty) 'value',
if (list.isNotEmpty) 'value', // LINT: Avoid duplicate collection elements. Try removing the duplicate element or replacing it with a different element.
];
final map = {
...{
'key': 'value', // LINT: Avoid duplicate collection elements. Try removing the duplicate element or replacing it with a different element.
},
...{
'key': 'value', // LINT: Avoid duplicate collection elements. Try removing the duplicate element or replacing it with a different element.
},
};
}
✅ Good:
void fn() {
final list = <String>[...];
final anotherList = [
...list,
if (list.isNotEmpty) 'value', // Correct, no duplicates
];
final map = {
...{
'key': 'value',
},
};
}
Example with "ignore-literals"
Config
analysis_options.yaml
dart_code_metrics:
rules:
- avoid-duplicate-collection-elements:
ignore-literals: true
✅ Good:
void fn() {
final list = [
'value',
'value', // Correct, ignored string literal
];
}