deepEquals static method

bool deepEquals(
  1. dynamic a,
  2. dynamic b
)

Returns true when a and b are structurally equal.

Maps and Lists are compared element-by-element and recursively, so nested collections need not share identity. Useful for shouldRebuild checks and state comparisons where == only does reference equality.

Obj.deepEquals({'a': [1, 2]}, {'a': [1, 2]}); // true

Implementation

static bool deepEquals(dynamic a, dynamic b) {
  if (identical(a, b)) return true;
  if (a is Map && b is Map) {
    if (a.length != b.length) return false;
    for (final k in a.keys) {
      if (!b.containsKey(k)) return false;
      if (!deepEquals(a[k], b[k])) return false;
    }
    return true;
  }
  if (a is List && b is List) {
    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;
  }
  return a == b;
}