mapOrNull<TResult extends Object?> method

  1. @optionalTypeArgs
TResult? mapOrNull<TResult extends Object?>({
  1. TResult? idle(
    1. Idle<T, F> value
    )?,
  2. TResult? loading(
    1. Loading<T, F> value
    )?,
  3. TResult? error(
    1. Error<T, F> value
    )?,
  4. TResult? success(
    1. Success<T, F> value
    )?,
})

A variant of map that fallback to returning null.

It is equivalent to doing:

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

Implementation

@optionalTypeArgs TResult? mapOrNull<TResult extends Object?>({TResult? Function( Idle<T, F> value)?  idle,TResult? Function( Loading<T, F> value)?  loading,TResult? Function( Error<T, F> value)?  error,TResult? Function( Success<T, F> value)?  success,}){
final _that = this;
switch (_that) {
case Idle() when idle != null:
return idle(_that);case Loading() when loading != null:
return loading(_that);case Error() when error != null:
return error(_that);case Success() when success != null:
return success(_that);case _:
  return null;

}
}