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

count

Counts how many elements a lazy pipeline produces.

int count<A>(Iterable<A> iterable) Future<int> countAsync<A>(FxAsyncIterable<A> iterable) int Fx.length // sync chain: inherited Iterable getter, no parens Future<int> FxAsync.count() // async chain method int size<A>(Iterable<A> iterable) // FxTS alias

Lecture

count is a terminal operator that counts elements by walking through all of them — there's no shortcut, because the upstream pipeline might be a lazily-computed map/filter chain with no fixed length until it's actually pulled. That means calling count on a filtered million-element range really does iterate all million values; it just doesn't build a List to do it. count is the Dart-idiomatic name; fxdart also accepts the FxTS spelling size — they're the same operator.

It works on any element type — unlike the numeric terminals (sum, min, max, average), count doesn't care what's inside the iterable, only how many there are.

On the sync chain, count is Dart's inherited Iterable.length getter (no parens) — since Fx is an Iterable, fx(pipeline).length walks the chain and returns the total. Use the top-level count(iterable), or .count() on the async chain, when you'd rather write it as a named operator. If you already have a concrete List, its .length is free — reach for count specifically when you're counting the output of a lazy chain without wanting to materialize it into a List first via toList.

Demo 1 · Basics

Demo 2 · Async

Try it yourself

Exercise: count how many entries are out of stock (value == 0).

Related: average — uses a count-like tally internally · isEmpty — a cheaper check when you only need to know "any at all?" · toList — materialize instead of just counting