Dart – Check Equality for Lists
Two Dart lists are equal by value when they have the same length and each element at a given index is equal to the element at the same index in the other list. The order of elements matters.
For example, [1, 2, 3] and [1, 2, 3] are element-wise equal, while [1, 2, 3] and [3, 2, 1] are not.
Why the == Operator Does Not Compare Dart List Elements
Using list1 == list2 does not normally compare the contents of two separate Dart lists. A standard List uses object equality, so two different list objects can contain identical elements and still produce false.
void main() {
final list1 = [10, 20, 30];
final list2 = [10, 20, 30];
print(list1 == list2);
}
Output
false
To compare the values, check the lengths first and then compare corresponding elements.
Check if Two Dart Lists Are Equal Element by Element
In this example, we take three lists. The first two contain equal values in the same order, while the third contains different values.
The areListsEqual() function first confirms that both arguments are lists and that their lengths match. It then compares one pair of elements at a time. The function returns false as soon as it finds a difference.
Dart Program
bool areListsEqual(var list1, var list2) {
// check if both are lists
if(!(list1 is List && list2 is List)
// check if both have same length
|| list1.length!=list2.length) {
return false;
}
// check if elements are equal
for(int i=0;i<list1.length;i++) {
if(list1[i]!=list2[i]) {
return false;
}
}
return true;
}
void main(){
List list1 = [24, 'Hello', 84];
List list2 = [24, 'Hello', 84];
List list3 = [11, 'Hi', 41];
if(areListsEqual(list1, list2)) {
print('list1 and list2 are equal in value.');
} else {
print('list1 and list2 are not equal in value.');
}
if(areListsEqual(list1, list3)) {
print('list1 and list3 are equal in value.');
} else {
print('list1 and list3 are not equal in value.');
}
}
Output
list1 and list2 are equal.
list1 and list3 are not equal.
Reusable Typed Function for Dart List Equality
A typed function is preferable when both inputs are already known to be lists. It avoids accepting unrelated values and works with lists of integers, strings, objects, or other element types.
bool listsEqual<T>(List<T> first, List<T> second) {
if (identical(first, second)) {
return true;
}
if (first.length != second.length) {
return false;
}
for (var i = 0; i < first.length; i++) {
if (first[i] != second[i]) {
return false;
}
}
return true;
}
void main() {
print(listsEqual([1, 2, 3], [1, 2, 3]));
print(listsEqual(['a', 'b'], ['a', 'c']));
print(listsEqual([1, 2], [2, 1]));
}
Output
true
false
false
The initial identical() check is a small optimization: when both variables refer to the same list object, no element-by-element comparison is needed.
Compare Dart Lists with ListEquality from package:collection
For application code, the collection package provides ListEquality. Add the package to the project, import it, and call equals().
dart pub add collection
import 'package:collection/collection.dart';
void main() {
const equality = ListEquality<int>();
final first = [4, 8, 12];
final second = [4, 8, 12];
final third = [4, 12, 8];
print(equality.equals(first, second));
print(equality.equals(first, third));
}
Output
true
false
ListEquality performs an ordered comparison. Therefore, lists with the same values in a different order are not equal.
Compare Nested Dart Lists with DeepCollectionEquality
A simple element-wise function is not sufficient for nested lists because each inner list is itself a separate object. Use DeepCollectionEquality when list elements can contain other lists, sets, maps, or nested combinations of collections.
import 'package:collection/collection.dart';
void main() {
const deepEquality = DeepCollectionEquality();
final first = [
[1, 2],
[3, 4]
];
final second = [
[1, 2],
[3, 4]
];
print(deepEquality.equals(first, second));
}
Output
true
Dart List Equality with Custom Objects
Element-wise list comparison relies on the equality behavior of each element. When a list contains custom objects, those objects must implement meaningful == and hashCode members if instances with the same field values should be considered equal.
Without custom object equality, two separately created objects can hold the same data but still compare as unequal. This affects both a manual loop and ListEquality.
Performance of Element-Wise List Comparison in Dart
Comparing two lists takes up to O(n) time, where n is the number of elements. The comparison can finish earlier when the lengths differ or when a mismatched pair is found near the beginning. The manual function uses O(1) additional space.
Common Questions About Dart List Equality
Does list1 == list2 compare all elements in Dart?
No. For normal Dart lists, == generally checks whether the operands represent the same list object rather than comparing all contained values.
Does the order of elements matter when comparing Dart lists?
Yes. Element-wise list equality is ordered. The lists [1, 2, 3] and [3, 2, 1] are not equal even though they contain the same values.
How do I compare nested lists in Dart?
Use DeepCollectionEquality from package:collection, or write a recursive comparison that checks each nested collection.
Can ListEquality compare lists of custom objects?
Yes, but the result depends on how the custom objects implement equality. Override == and hashCode, or supply a suitable equality strategy.
Summary of Dart List Equality Methods
Use a length check and an index-based loop for a small, dependency-free comparison. Use ListEquality for reusable ordered list comparison and DeepCollectionEquality for nested collections. In this Dart Tutorial, we learned how to check whether two lists are equal by value and how list order, nested collections, and custom object equality affect the result.
TutorialKart.com