elementsEqual<T> function

bool elementsEqual<T>(
  1. List<T> a,
  2. List<T> b
)

Whether a and b have the same length and pairwise == elements.

Works for any list the generated layer holds a field in, typed lists included (Int64List, Float64List and friends all implement List), and walks no further than the first difference.

Two IEEE-754 properties are inherited from == on double and are worth naming, because this is what decides whether a field is written:

  • NaN != NaN, so a list holding a NaN never equals its default — the field is written out rather than omitted, which is the safe direction: the value survives.
  • -0.0 == 0.0, so a list of negative zeroes does equal an all-zero default and the field is omitted, which loses the sign bit. The wire format keeps -0.0 faithfully; a default comparison cannot, because the two are equal in the language.

A float array's default test must not use this: floatBitsEqual compares the bit patterns instead.

Implementation

bool elementsEqual<T>(List<T> a, List<T> b) {
  if (identical(a, b)) return true;
  final n = a.length;
  if (n != b.length) return false;
  for (var i = 0; i < n; i++) {
    if (a[i] != b[i]) return false;
  }
  return true;
}