first method

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

Returns first element.

Use predicate to return the first element matching the given predicate

@throws NoSuchElementException if the collection is empty.

Implementation

T first([bool Function(T)? predicate]) {
  if (predicate == null) {
    if (isEmpty()) throw const NoSuchElementException("List is empty.");
    return get(0);
  } else {
    for (final element in iter) {
      if (predicate(element)) return element;
    }
    throw const NoSuchElementException(
        "Collection contains no element matching the predicate.");
  }
}