firstOrNull method

T? firstOrNull(
  1. [bool predicate(
    1. T
    )?]
)

Returns the first element (matching predicate when provided), or null if the collection is empty.

Implementation

T? firstOrNull([bool Function(T)? predicate]) {
  if (predicate == null) {
    if (this is KtList) {
      final list = this as KtList<T>;
      if (list.isEmpty()) {
        return null;
      } else {
        return list[0];
      }
    }
    final i = iterator();
    if (!i.hasNext()) {
      return null;
    }
    return i.next();
  } else {
    for (final element in iter) {
      if (predicate(element)) return element;
    }
    return null;
  }
}