ifEmpty
Switches to a fallback when the pipeline turns out to be empty — decided lazily, at iteration time.
Lecture
A pipeline that might produce nothing usually forces a
detour: materialize it, test isEmpty, branch. That
breaks the chain and — worse — runs the pipeline before you meant to.
ifEmpty(fallback) folds the branch into the pipeline
itself: values pass through untouched, and only if the source
finishes without yielding anything does the fallback iterable take
over. The fallback function is not even called otherwise —
lazy in both directions.
defaultIfEmpty(value) is the one-value shorthand: the
placeholder row, the 0 for an empty report, the
"no results" marker. Both compose anywhere in the chain, which
matters after aggressive filtering — the emptiness is decided by
whatever reaches this point, not by the original source.
fxdart extension (no FxTS counterpart), after Rx's
switchIfEmpty / defaultIfEmpty. The async
forms accept an async fallback chain and a Future
default, and compose with
concurrent.
Demo 1 · A default for the empty report
Demo 2 · Fallback source, never touched otherwise
Try it yourself
Exercise: search with a fallback query.