hashAll static method Null safety

  1. @Since("2.14")
int hashAll(
  1. Iterable<Object?> objects
)

Creates a combined hash code for a sequence of objects.

The hash code is computed for elements in objects, even if they are null, by numerically combining the Object.hashCode of each element in iteration order.

The result of hashAll([o]) is not o.hashCode.

Example:

class SomeObject {
  final List<String> path;
  SomeObject(this.path);
  bool operator ==(Object other) {
    if (other is SomeObject) {
      if (path.length != other.path.length) return false;
      for (int i = 0; i < path.length; i++) {
        if (path[i] != other.path[i]) return false;
      }
      return true;
    }
    return false;
  }

  int get hashCode => Object.hashAll(path);
}

The computed value will be consistent when the function is called again with objects that have the same hash codes in the same order during an execution of a single program.

The hash value generated by this function is not guaranteed to be stable over different runs of the same program, or between code run in different isolates of the same program. The exact algorithm used may differ between different platforms, or between different versions of the platform libraries, and it may depend on values that change on each program execution.

Implementation

@Since("2.14")
static int hashAll(Iterable<Object?> objects) {
  int hash = _hashSeed;
  for (var object in objects) {
    hash = SystemHash.combine(hash, object.hashCode);
  }
  return SystemHash.finish(hash);
}