avoid_duplicate_collection_elements
This rule is in the pedantic preset.
This rule flags a collection literal that contains the same element more than once. The quick fix removes the duplicate, keeping the first occurrence.
Why use this rule
Section titled “Why use this rule”A hand-written literal listing the same value twice is almost always a mistake — one entry was meant to be a different constant, enum value, or field. Nothing fails: the list simply carries a duplicate, and whatever consumes it does the work twice or shows the entry twice.
Repeated spreads and repeated if elements are reported too. Writing ...items twice either duplicates every value or is dead weight, and the same holds for two identical if elements.
Plain values in sets and maps are deliberately out of scope. The analyzer already reports duplicate set elements and duplicate map keys natively, so covering them here would double-report. Spreads inside a set or map are checked, since the analyzer does not catch those.
const supportedLocales = [ Locale('en'), Locale('de'), Locale('en'),];
final combined = [...base, ...base];
final entries = [ if (items.isNotEmpty) 'value', if (items.isNotEmpty) 'value',];const supportedLocales = [ Locale('en'), Locale('de'), Locale('fr'),];
final combined = [...base, ...extra];
final entries = [ if (items.isNotEmpty) 'value', if (items.isEmpty) 'empty',];Known limitations
Section titled “Known limitations”Elements are compared by source text, which is only sound for expressions that always evaluate the same way. The rule therefore compares only literals, identifiers, and property accesses — [next(), next()] is never reported, since two calls may legitimately produce different values. The same applies to spreads: [...fetch(), ...fetch()] is left alone.
Turning this rule off
Section titled “Turning this rule off”This rule appears only in the pedantic preset because repeated values and
actions are common in fixtures, ordered scenarios, gradients and other
intentionally positional lists.
Enable it by name:
rules: avoid_duplicate_collection_elements: trueTo keep the rule on but skip certain paths, use per-rule exclude.
Options
Section titled “Options”many_lints: rules: avoid_duplicate_collection_elements: ignore_literals: truerules: avoid_duplicate_collection_elements: ignore_literals: true| Option | Type | Default | Description |
|---|---|---|---|
ignore_literals |
bool | false |
Skip repeated literals ([0, 0, 0]), still reporting duplicated identifiers and property accesses |
Related rules
Section titled “Related rules”avoid_collection_equality_checks— Avoid comparing collections with == or != as it checks reference equality, not contents.avoid_collection_methods_with_unrelated_types— Avoid calling collection methods with arguments whose types are unrelated to the collection’s type parameter.avoid_unsafe_collection_methods— Check for emptiness before using first, last, single or reduce.avoid_accessing_collections_by_constant_index— Avoid accessing a collection by a constant index inside a loop.