orElse method

T orElse(
  1. T compute()
)

Returns the value when non-null, otherwise the result of compute.

compute is evaluated lazily — only when the receiver is null — so an expensive fallback is skipped when not needed. Equivalent to this ?? but accepting a thunk instead of a precomputed value.

Example:

const String? name = null;
name.orElse(() => 'Anonymous'); // 'Anonymous'

Implementation

T orElse(T Function() compute) => this ?? compute();