distinctUntilChanged method

Stream<T> distinctUntilChanged([
  1. bool equals(
    1. T,
    2. T
    )?
])

Emits only values that differ from the previous emission. Optionally compare by a derived key instead of the value itself.

stream.distinct((a, b) => a.id == b.id)

Implementation

Stream<T> distinctUntilChanged([bool Function(T, T)? equals]) async* {
  T? prev;
  var hasPrev = false;
  await for (final value in this) {
    if (!hasPrev || !(equals?.call(prev as T, value) ?? (prev == value))) {
      prev = value;
      hasPrev = true;
      yield value;
    }
  }
}