nonNulls

Filters out null and narrows the element type from T? to T at the same time.

Iterable<A> nonNulls<A>(Iterable<A?> iterable) FxAsyncIterable<A> nonNullsAsync<A>(FxAsyncIterable<A?> iterable) Iterable<A> Iterable.nonNulls // inherited getter (no parens); sync chain only Iterable<A> compact<A>(Iterable<A?> iterable) // FxTS alias

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 compact — they're the same operator. FxTS's compact 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 pluck or any lookup that returns T?: nonNulls(pluck(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 nonNullsAsync(...) (or its FxTS alias compactAsync) 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.

Related: pluck — a common source of nullable values · reject — drop by arbitrary predicate · compactObject — the Map-keyed equivalent · uniq — remove duplicates