singleWhereOrNull method

E? singleWhereOrNull(
  1. bool test(
    1. E element
    )
)

Returns the single element passing the given test, or null if element was not found or more than one element was found.

Implementation

E? singleWhereOrNull(bool Function(E element) test) {
  var found = false;
  E? single;

  for (final element in this) {
    if (test(element)) {
      if (found) return null;

      single = element;
      found = true;
    }
  }

  return single;
}