firstOrNullWhere method

E? firstOrNullWhere(
  1. bool predicate(
    1. E element
    )
)

Returns the first element matching the given predicate, or null if no such element was found.

final list = ['a', 'Test'];
final firstLong= list.firstOrNullWhere((e) => e.length > 1); // 'Test'
final firstVeryLong = list.firstOrNullWhere((e) => e.length > 5); // null

Implementation

E? firstOrNullWhere(bool Function(E element) predicate) {
  for (final element in this) {
    if (predicate(element)) return element;
  }
  return null;
}