last method
Returns the last element matching the given predicate
.
@throws NoSuchElementException if no such element is found.
Implementation
T last([bool Function(T)? predicate]) {
if (predicate == null) {
if (isEmpty()) throw const NoSuchElementException("List is empty.");
return get(lastIndex);
} else {
final i = listIterator(size);
if (!i.hasPrevious()) {
throw const NoSuchElementException("Collection is empty");
}
while (i.hasPrevious()) {
final element = i.previous();
if (predicate(element)) {
return element;
}
}
throw const NoSuchElementException(
"Collection contains no element matching the predicate.");
}
}