description

# lastWhere method

E lastWhere(

1. bool test(
   1. E value
   ), {
2. E orElse()?,

})
override

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.

Example:

    final numbers = <int>[1, 2, 3, 5, 6, 7];
    var result = numbers.lastWhere((element) => element < 5); // 3
    result = numbers.lastWhere((element) => element > 5); // 7
    result = numbers.lastWhere((element) => element > 10,
        orElse: () => -1); // -1

If no element satisfies `test`, the result of invoking the `orElse` function is returned. If `orElse` is omitted, it defaults to throwing a StateError.

## Implementation

    E lastWhere(bool test(E value), {E Function()? orElse}) {
      var iterator = this.iterator;
      E result;
      do {
        if (!iterator.moveNext()) {
          if (orElse != null) return orElse();
          throw IterableElementError.noElement();
        }
        result = iterator.current;
      } while (!test(result));
      while (iterator.moveNext()) {
        var current = iterator.current;
        if (test(current)) result = current;
      }
      return result;
    }