firstWhere method

E firstWhere (
  1. bool test(
    1. E value
    ),
  2. {E orElse( )}
)
override

Returns the first element that satisfies the given predicate test.

Iterates through elements and returns the first to satisfy test.

If no element satisfies test, the result of invoking the orElse function is returned. If orElse is omitted, it defaults to throwing a StateError.

Implementation

E firstWhere(bool test(E value), {E Function()? orElse}) {
  for (E element in this) {
    if (test(element)) return element;
  }
  if (orElse != null) return orElse();
  throw IterableElementError.noElement();
}

© 2012 the Dart project authors
Licensed under the Creative Commons Attribution-ShareAlike License v4.0.
https://api.dart.dev/stable/2.13.0/dart-collection/SetMixin/firstWhere.html