firstWhereOrNull method

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

Returns the first element that satisfies the given test function, or null if no element matches.

Iterates through each element in the list, and applies the test function. If an element satisfies the test, it is immediately returned, ending the iteration. If no element satisfies the test, the function returns null.

Example:

List<int> numbers = [1, 3, 5, 7, 9];
int? evenNumber = numbers.firstWhereOrNull((num) => num % 2 == 0);
print(evenNumber); // Output: null

test: The function to test each element for a condition.

Implementation

T? firstWhereOrNull(bool Function(T) test) {
  for (T element in this) {
    if (test(element)) return element;
  }

  return null;
}