Skip to main content

avoid-collection-equality-checks

added in: 1.17.0

Warns when a collection is checked for equality with another collection.

Collections in Dart have no inherent equality. Two sets are not equal, even if they contain exactly the same objects as elements.

To compare for equality, use custom deep equality checks (or one of the existing packages, e.g. collection).

Example

❌ Bad:

void fn() {
const list1 = [1, 2, 3];
const list2 = [1, 2, 3];

final anotherList1 = [1, 2, 3];
final anotherList2 = [1, 2, 3];

print(anotherList1 == anotherList2); // LINT
print(anotherList1 == list1); // LINT
}

✅ Good:

void fn() {
const list1 = [1, 2, 3];
const list2 = [1, 2, 3];

print(list1 == list2);

final anotherList1 = [1, 2, 3];
final anotherList2 = [1, 2, 3];

ListEquality().equals(list1, anotherList1);
}