lastOrNull method

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

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

Implementation

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