Dart/Flutter - Compare Two Lists for Equality

In this tutorial, I am going to show you how to compare whether two Lists are equal in Dart.

List is a data type that contains a collection of elements. It's allowed to contain the same element multiple times and the order of the elements also matters. If you have two Lists and you want to compare that they are equal, you can see the examples in this tutorial. Two Lists are considered equal if they have the same number of elements and each element at a particular index is equal to the other List's element at the same index. In addition, if both Lists are null, they're considered equal as well.

Below are the Lists that we are going to compare.

  List<String> l1 = ['a', 'b', 'c', 'd', 'e'];
  List<String> l2 = ['a', 'b', 'c', 'd', 'e'];
  List<String> l3 = ['a', 'b', 'c'];
  List<String>? nl1;
  List<String>? nl2;

The expected results are:

  • l1 is equal to l2
  • l1 is not equal to l3
  • l1 is not equal to nl1
  • nl1 is equal to nl2

Without Library

If your Dart application doesn't use Flutter framework and you don't want to use an additional library, the solution is to write a method for comparison.

Before comparing each element, you can use the equality operator (==). If it returns true, it means either both Lists refer to the same memory reference or both variables are null. For those cases, we can say that they are equal and the comparison should return true. Next, we check if only one of them is null or if the number of elements is not equal, the comparison should return false. Otherwise, we need to compare each element.

  bool areListsEqual(List? a, List? b) {
if (a == b) { return true; } if (a == null || b == null || a.length != b.length) { return false; } for (int i = 0; i < a.length; i++) { if (a[i] != b[i]) { return false; } } return true; }

Usage Examples:

  print(areListsEqual(l1, l2));
print(areListsEqual(l1, l3));
print(areListsEqual(l1, nl1));
print(areListsEqual
(nl1, nl2));

Output:

  true
  false
  false
  true

Using quiver

quiver is a utility library for Dart. It has a method named listsEqual which checks the equality of two Lists. First, add it to the pubspec.yaml file and run dart pub get (or flutter pub get for Flutter).

  dependencies:
    quiver: ^3.2.1

The listsEqual method is available to use by importing package:quiver/collection.dart.

  print(listsEqual(l1, l2));
  print(listsEqual(l1, l3));
  print(listsEqual(l1, nl1));
  print(listsEqual(nl1, nl2));

Output:

  true
  false
  false
  true

Using Flutter foundation

If you use Flutter, you don't need any additional library or writing your own code for checking List equality. Flutter's foundation library already has a method named listEquals for that purpose. It's available to use by importing package:flutter/foundation.dart.

  print(listEquals(l1, l2));
  print(listEquals(l1, l3));
  print(listEquals(l1, nl1));
  print(listEquals(nl1, nl2));

Output:

  true
  false
  false
  true

Compare Lists of Mutable Objects

Comparing List of mutable objects can be more tricky. When comparing each element, the methods above use equality (==) operator. By default, comparing two mutable objects with different memory references will result in false. If that's what you want, you can go with any of the methods above without any additional thing to do.

For example, we have the following class.

  class Item {
    String name;
    double price;

    Item({
      required this.name,
      required this.price,
    });
  }

There are two Lists with each element referring to a different object. However, the elements at the same index actually have the same values for all fields.

  final Item item1a = Item(name: 'A', price: 100);
  final Item item1b = Item(name: 'B', price: 200);
  List<Item> items = [item1a, item1b];

  final Item item2a = Item(name: 'A', price: 100);
  final Item item2b = Item(name: 'B', price: 200);
  List<Item> items2 = [item2a, item2b];

  print(isEqualUsingLoop(items, items2));

Output:

  false

As you can see from the output, the two Lists are considered different because the equality operator of the elements at the same index returns false. If you want to make the equality operator returns true if the fields have equal values, you can override it. If you override the equality operator, you also need to override the hashCode property.

  class Item {
    String name;
    double price;

    Item({
      required this.name,
      required this.price,
    });

    @override
    bool operator ==(Object other) {
      if (identical(this, other)) {
        return true;
      }

      return (other is Item
          && other.runtimeType == runtimeType
          && other.name == name
          && other.price == price
      );
    }

    @override
    int get hashCode => Object.hash(name, price);
  }

After overriding the equality operator, the result will be true since the elements are now compared by the values of the fields.

Summary

In this tutorial, we have learned the code to compare that two Lists are equal in Dart. The similar code is also available from the quiver or foundation library. For a List of mutable objects, by default the elements are compared by the memory reference. If you prefer to compare by the values of the fields, it can be done by overriding the equality operator.

You can also read about: