singleWhereOrNull method

T? singleWhereOrNull(
  1. bool test(
    1. T
    )
)

Returns the single element that satisfies test, or null if none does.

Throws StateError if more than one element satisfies test.

Implementation

T? singleWhereOrNull(bool Function(T) test) {
  T? result;
  var found = false;
  for (final element in this) {
    if (test(element)) {
      if (found) throw StateError('More than one element matches test');
      result = element;
      found = true;
    }
  }
  return result;
}