collection_identity_comparison
Reports == or != between two collections, which compares identity, not contents. Compare the contents, or write identical.
- Tier
- advisory
- Scope
- Dart
- Lowest preset
- recommended
- Fix
- none
What it reports
Section titled “What it reports”A comparison with == or != of two collections whose equality is the SDK’s: selected == [1, 2]. That equality tests whether the two are the same object, not whether they hold the same elements, so two lists of the same elements are not equal.
Both sides must be collections: a value whose static type is an Iterable, lists and sets among them, or a Map. A type that declares its own == outside the SDK is left alone, since an immutable list of a package may compare its contents.
It does not report:
- a comparison inside a method, a function or an argument that
identity_methodsnames. By default these areoperator ==and the methods and arguments that compare an old and a new value, where identity is meant:didUpdateWidget,shouldRepaint,shouldRebuild,shouldReclip,shouldRelayout,updateShouldNotify,buildWhenandlistenWhen; - a comparison with a constant,
items == const [], since a constant is one object; - a comparison of a collection with a value of another type, such as
Object.
An identity test meant outside the places of identity_methods is reported. A justified suppression says it is meant.
== between two lists reads as a comparison of contents, which is what other languages and libraries give. In Dart it is an identity test: if (tags == ['new']) is never true, and if (old != current) is true for every copy, even an equal one.
The rule is advisory because the identity test is sometimes meant, outside the places the rule knows.
Example
Section titled “Example”bool isDefault(List<String> tags) => tags == ['new'];import 'package:collection/collection.dart';
bool isDefault(List<String> tags) => const ListEquality<String>().equals(tags, ['new']);In Flutter code listEquals, setEquals and mapEquals of package:flutter/foundation.dart do the same. Where the same object is meant, identical(a, b) says so.
Rule parameters
Section titled “Rule parameters”Set under the assay: key of the project’s root analysis_options.yaml.
| Rule parameter | Default | Effect |
|---|---|---|
identity_methods |
[==, didUpdateWidget, shouldRepaint, shouldRebuild, shouldReclip, shouldRelayout, updateShouldNotify, buildWhen, listenWhen] |
The methods, functions and named arguments in which a comparison by identity is meant. == stands for an operator ==. No comparison inside one of them is reported. Your list replaces the default list. |
enabled |
true |
false switches the rule off for the whole project. |
exclude |
[] |
Path globs where the rule is silent. |
assay: rules: collection_identity_comparison: identity_methods: ["==", didUpdateWidget, shouldRepaint, selectWhen]Suppressing it
Section titled “Suppressing it”Write why on the line directly above the suppression:
// The cache hands out the same list until it is refreshed.// ignore: assay/collection_identity_comparison// ignore_for_file: assay/collection_identity_comparison silences it in a whole file, and // ignore: collection_identity_comparison without the prefix works too. See Suppressions.