mapEquals<T, U> function Null safety

bool mapEquals <T, U>(
  1. Map<T, U>? a,
  2. Map<T, U>? b
)

Compares two maps for deep equality.

Returns true if the maps are both null, or if they are both non-null, have the same length, and contain the same keys associated with the same values. Returns false otherwise.

The term "deep" above refers to the first level of equality: if the elements are maps, lists, sets, or other collections/composite objects, then the values of those elements are not compared element by element unless their equality operators (Object.==) do so.

See also:

  • setEquals, which does something similar for sets.
  • listEquals, which does something similar for lists.

Implementation

bool mapEquals<T, U>(Map<T, U>? a, Map<T, U>? b) {
  if (a == null)
    return b == null;
  if (b == null || a.length != b.length)
    return false;
  if (identical(a, b))
    return true;
  for (final T key in a.keys) {
    if (!b.containsKey(key) || b[key] != a[key]) {
      return false;
    }
  }
  return true;
}