deepEquals function

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

Deep equality: compares maps and lists recursively; primitives by value.

Recurses to the inputs' nesting depth, so very deeply nested data can exhaust the call stack — avoid on untrusted input of unbounded depth.

Implementation

bool deepEquals(Object? a, Object? b) {
  if (identical(a, b)) return true;
  if (a == null || b == null) return false;
  if (a is Map && b is Map) {
    // Equal lengths plus every key of a present (and equal) in b implies the key
    // sets match, so there is no need to also scan b's keys against a.
    if (a.length != b.length) return false;
    for (final Object? 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 (int i = 0; i < a.length; i++) {
      if (!deepEquals(a[i], b[i])) return false;
    }
    return true;
  }
  return a == b;
}