whenOrNull<TResult extends Object?> method

  1. @optionalTypeArgs
TResult? whenOrNull<TResult extends Object?>({
  1. TResult? never()?,
  2. TResult? timeout(
    1. int data
    )?,
  3. TResult? timestamp(
    1. int data
    )?,
})

A variant of when that fallback to returning null

It is equivalent to doing:

switch (sealedClass) {
  case Subclass(:final field):
    return ...;
  case _:
    return null;
}

Implementation

@optionalTypeArgs TResult? whenOrNull<TResult extends Object?>({TResult? Function()?  never,TResult? Function( int data)?  timeout,TResult? Function( int data)?  timestamp,}) {final _that = this;
switch (_that) {
case ExpirationNever() when never != null:
return never();case ExpirationTimeout() when timeout != null:
return timeout(_that.data);case ExpirationTimestamp() when timestamp != null:
return timestamp(_that.data);case _:
  return null;

}
}