singleWhere method

view source

E singleWhere (bool test(E element), {E orElse()}) inherited

Implementation

E singleWhere(bool test(E element), {E orElse()?}) {
  late E result;
  bool foundMatching = false;
  for (E element in this) {
    if (test(element)) {
      if (foundMatching) {
        throw IterableElementError.tooMany();
      }
      result = element;
      foundMatching = true;
    }
  }
  if (foundMatching) return result;
  if (orElse != null) return orElse();
  throw IterableElementError.noElement();
}