Esta página ainda não foi traduzida, por isso é exibida em inglês. Ajude a traduzir

fxUnless

Applies a callback when a predicate fails, otherwise returns the value unchanged.

T fxUnless<T>(bool Function(T) predicate, T Function(T) callback, T value)

Lecture

fxUnless is fxWhen with the condition flipped: fxUnless(predicate, callback, value) runs callback(value) when predicate(value) is false, and returns value untouched when the predicate holds. It reads well for "fill in a default unless this condition is already satisfied" — validation, backfilling missing data, normalizing edge cases.

Like fxWhen, both branches must return the same type T in Dart (no union return types), and there's no chain form — it's a plain data-first function.

Demo 1 · Basics

Demo 2 · Backfilling missing data

Try it yourself

Exercise: use fxUnless to fill in 'general' for any tag that hasn't been set.

Related: fxWhen — the inverse-condition sibling of unless · fxCases — multiple predicates instead of just one · fxThrowIf — throw instead of substituting a value · fxCompact — drop missing values instead of filling them in