avoid_collection_equality_checks
v0.3.0 Warning Collection & Type
Collections in Dart (List, Set, Map) use reference equality by default, not structural equality. Comparing two collections with == or != almost never produces the intended result because two distinct instances with identical contents are not considered equal.
Why use this rule
Section titled “Why use this rule”Using == on collections is a common source of bugs. Two lists with the same elements will return false when compared with == because they are different objects in memory. Use DeepCollectionEquality from the collection package or compare individual elements instead.
See also: Dart collections | collection package | Dart lint: unrelated_type_equality_checks
void example() { final list1 = [1, 2, 3]; final list2 = [1, 2, 3];
// Reference equality, not deep equality final same = list1 == list2; // always false!
final set1 = {1, 2}; final set2 = {1, 2};
// Same problem with sets final sameSet = set1 == set2;
final map1 = {'a': 1}; final map2 = {'a': 1};
// Same problem with maps final sameMap = map1 != map2;}void example() { // Const collections are fine — they are canonicalized final same = const [1, 2] == const [1, 2]; // true
// Null checks are fine final List<int>? maybeList = null; final isNull = maybeList == null;
// Non-collection equality is fine final a = 1; final b = 1; final sameInt = a == b;
// Use DeepCollectionEquality from the `collection` package: // import 'package:collection/collection.dart'; // final eq = DeepCollectionEquality().equals(list1, list2);}Configuration
Section titled “Configuration”This rule is in the core preset, so it is on with preset: core,
preset: recommended or preset: opinionated.
To turn it off:
rules: avoid_collection_equality_checks: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”prefer_overriding_parent_equality— Override == and hashCode when the parent class overrides them.list_all_equatable_fields— Ensure all fields are listed in Equatable props.prefer_equatable_mixin— Prefer using EquatableMixin instead of extending Equatable.avoid_collection_methods_with_unrelated_types— Avoid calling collection methods with arguments whose types are unrelated to the collection’s type parameter.