Esta página ainda não foi traduzida, por isso é exibida em inglês. Ajude a traduzir

takeLast

Returns a lazy iterable of the last length values from a source.

Iterable<A> takeLast<A>(int length, Iterable<A> iterable) FxAsyncIterable<A> takeLastAsync<A>(int length, FxAsyncIterable<A> iterable) Fx<T> Fx.takeLast(int count) // chain FxAsync<T> FxAsync.takeLast(int count) Iterable<A> takeRight<A>(...) // FxTS alias

Lecture

takeLast is take's mirror image: instead of the first length values, you get the last ones. There's a catch, though — to know what the "last" values are, takeLast has to see every element first. It materializes the whole source into a list internally before yielding anything, so unlike most FxDart operators it does not work on infinite sequences. takeLast is the Dart-idiomatic name; fxdart also accepts the FxTS spelling takeRight — they're the same operator.

The async version has the same constraint: takeLastAsync drains the entire upstream (awaiting every element) before it can hand back the tail. Reach for it only when you know the source is finite and small enough to buffer.

Demo 1 · Basics

Demo 2 · Async (must buffer the whole source first)

Try it yourself

Exercise: keep only the last 3 scores.

Related: take — first n instead of last n · dropRight — drop the last n · reverse — also materializes the source · slice — arbitrary index window