singleWhereOrNull method

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

Returns the single element matching test, or null if zero or more than one match.

Implementation

T? singleWhereOrNull(bool Function(T element) test) {
  T? result;
  var matchCount = 0;
  for (final element in this) {
    if (test(element)) {
      matchCount++;
      if (matchCount > 1) return null;
      result = element;
    }
  }
  return matchCount == 1 ? result : null;
}