本页尚未翻译,因此以英文显示。 参与翻译

slice

Returns the values between index start (inclusive) and end (exclusive).

Iterable<A> slice<A>(int start, Iterable<A> iterable, [int? end]) FxAsyncIterable<A> sliceAsync<A>(int start, FxAsyncIterable<A> iterable, [int? end]) Fx<T> Fx.slice(int start, [int? end]) // chain FxAsync<T> FxAsync.slice(int start, [int? end])

Lecture

slice is a general index window: it yields the elements whose position falls in [start, end), and omitting end means "to the end of the source". It's a single operator that subsumes both drop (via start) and take (via end) at once.

Watch the argument order in the data-first form — unlike most FxDart functions, the iterable sits in the middle: slice(start, iterable, [end]), mirroring FxTS. The chain form doesn't have this quirk, since the iterable is the receiver: fx(iterable).slice(start, end). Under the hood slice is still just walking the source once and counting indices, so it stays lazy and works fine on an infinite source as long as you supply an end.

Demo 1 · Basics & argument order

Demo 2 · Async

Try it yourself

Exercise: use slice to grab the window from index 2 up to (not including) index 5.

Related: drop — just the start-index half of slice · take — just the count/end half of slice · chunk — fixed-size windows across the whole source