ifNonNull<R> method

R? ifNonNull<R>(
  1. R fn(
    1. T
    ), [
  2. R onNull()?
])

Executes the given function if the value is non-null. Analogous to synchronous Future.then

This method allows you to perform an operation on a value only if it is non-null.

Example:

int? value = 42;
value.ifNonNull((v) => print(v)); // Prints: 42

Implementation

R? ifNonNull<R>(R Function(T) fn, [R Function()? onNull]) => switch (this) {
  T value => fn(value),
  null => onNull?.call(),
};