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

unless

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

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

Lecture

unless is when with the condition flipped: unless(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 when, 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 unless to fill in 'general' for any tag that hasn't been set.

Related: when — the inverse-condition sibling of unless · cases — multiple predicates instead of just one · throwIf — throw instead of substituting a value · compact — drop missing values instead of filling them in