firstWhereOrNull method

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

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

Example:

Iterable<String>? words = ["Flutter", "Dart", "Java"];
String? result = words.firstWhereOrNull((word) => word.length == 5);  // "Dart"

Implementation

T? firstWhereOrNull(bool Function(T element) test) {
  if (isNullOrEmpty) {
    return null;
  }
  final list = this!.where(test);
  return list.isEmpty ? null : list.first;
}