count
Counts how many elements a lazy pipeline produces.
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).