deepEquals function

bool deepEquals(
  1. Object? a,
  2. Object? b
)

Whether a and b hold equal values, comparing lists (including typed data), sets, maps and other iterables element by element, and anything else with ==. A List<int> equals a Uint8List with the same bytes.

Implementation

bool deepEquals(Object? a, Object? b) {
  if (identical(a, b)) return true;
  switch ((a, b)) {
    case (List<Object?> a, List<Object?> b):
      if (a.length != b.length) return false;
      for (var i = 0; i < a.length; i++) {
        if (!deepEquals(a[i], b[i])) return false;
      }
      return true;
    case (Set<Object?> a, Set<Object?> b):
      return a.length == b.length && a.containsAll(b);
    case (Map<Object?, Object?> a, Map<Object?, Object?> b):
      if (a.length != b.length) return false;
      for (final key in a.keys) {
        if (!b.containsKey(key) || !deepEquals(a[key], b[key])) return false;
      }
      return true;
    case (Iterable<Object?> a, Iterable<Object?> b):
      return deepEquals(a.toList(), b.toList());
  }
  return a == b;
}