Skip to content

avoid_duplicate_collection_elements

v0.8.0WarningFixConfigurableCollection Type

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.

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',
];

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.

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:

many_lints.yaml
rules:
avoid_duplicate_collection_elements: true

To keep the rule on but skip certain paths, use per-rule exclude.

analysis_options.yaml
many_lints:
rules:
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