setEquals<T> function Null safety

bool setEquals<T>(
  1. Set<T>? a,
  2. Set<T>? b
)

Compares two sets for element-by-element equality.

Returns true if the sets are both null, or if they are both non-null, have the same length, and contain the same members. Returns false otherwise. Order is not compared.

If the elements are maps, lists, sets, or other collections/composite objects, then the contents of those elements are not compared element by element unless their equality operators (Object.==) do so. For checking deep equality, consider using DeepCollectionEquality class.

See also:

  • listEquals, which does something similar for lists.
  • mapEquals, which does something similar for maps.

Implementation

bool setEquals<T>(Set<T>? a, Set<T>? 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 value in a) {
    if (!b.contains(value)) {
      return false;
    }
  }
  return true;
}