match

R match<R>({

  1. required R some(
    1. T
    ),
  2. required R none(),
})

Pattern match on nullable value with cases for non-null and null.

This provides a safe way to handle nullable values by requiring explicit handling of both the present (some) and absent (none) cases.

Parameters:

  • some: Function called when this value is non-null
  • none: Function called when this value is null

Returns: The result of calling either some or none

Example:

int? maybeNumber = 42;

final result = maybeNumber.match( some: (n) => 'Number: $n', none: () => 'No number', ); // Returns: "Number: 42"

Implementation

R match<R>({required R Function(T) some, required R Function() none}) =>
    switch (this) {
      final T value => some(value),
      null => none(),
    };