Returns the last element that satisfies the given predicate test
.
An iterable that can access its elements directly may check its elements in any order (for example a list starts by checking the last element and then moves towards the start of the list). The default implementation iterates elements in iteration order, checks test(element)
for each, and finally returns that last one that matched.
If no element satisfies test
, the result of invoking the orElse
function is returned. If orElse
is omitted, it defaults to throwing a StateError.
E lastWhere(bool test(E element), {E orElse()}) { E result = null; bool foundMatching = false; for (E element in this) { if (test(element)) { result = element; foundMatching = true; } } if (foundMatching) return result; if (orElse != null) return orElse(); throw IterableElementError.noElement(); }
© 2012 the Dart project authors
Licensed under the Creative Commons Attribution-ShareAlike License v4.0.
https://api.dartlang.org/stable/2.0.0/dart-core/Iterable/lastWhere.html