avoid-collection-equality-checks
preset: recommended
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];
// LINT: Avoid checking mutable collections for equality.
print(anotherList1 == anotherList2);
// LINT: Avoid checking mutable collections for equality.
print(anotherList1 == list1);
}
✅ 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); // Correct, compared via 'ListEquality'
}