nonNulls
Filters out null and narrows the element type from T? to T at the same time.
Lecture
nonNulls takes an Iterable<A?> and
returns an Iterable<A> — every null is
dropped, and the type checker knows it: nothing downstream needs a
null check anymore. nonNulls is the Dart-idiomatic name;
fxdart also accepts the FxTS spelling fxCompact — they're the
same operator. FxTS's fxCompact drops all six JS falsy values
(undefined, null, 0,
'', NaN, false); Dart has no
single "falsy" concept, so the port only removes null —
a deliberate, narrower behavior that's easy to reason about.
It shows up constantly after fxPluck
or any lookup that returns T?: fxNonNulls(fxPluck(key, records))
gives you a clean, non-nullable list in one step.
On the sync chain, .nonNulls is an inherited
Iterable getter — write it with no parens
(fx(xs).nonNulls), and because it returns a plain
Iterable<A>, wrap it in fx(...) to keep
chaining. There is no async getter, so on an async pipeline use the
top-level fxNonNullsAsync(...) (or its FxTS alias
fxCompactAsync) and wrap with fxAsync(...).
Demo 1 · Basics & type narrowing
Demo 2 · Async, with concurrency
Try it yourself
Exercise: use nonNulls to drop the nulls from
answers.
fxPluck — a common source of nullable values ·
reject — drop by arbitrary predicate ·
fxCompactObject — the Map-keyed equivalent ·
uniq — remove duplicates