mapOrNull<TResult extends Object?> method

  1. @optionalTypeArgs
TResult? mapOrNull<TResult extends Object?>({
  1. TResult? item(
    1. UnitItem value
    )?,
  2. TResult? ssCC(
    1. UnitSSCC value
    )?,
  3. TResult? ph(
    1. UnitPH 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( UnitItem value)?  item,TResult? Function( UnitSSCC value)?  ssCC,TResult? Function( UnitPH value)?  ph,}){
final _that = this;
switch (_that) {
case UnitItem() when item != null:
return item(_that);case UnitSSCC() when ssCC != null:
return ssCC(_that);case UnitPH() when ph != null:
return ph(_that);case _:
  return null;

}
}