when<TResult extends Object?> method

  1. @optionalTypeArgs
TResult when<TResult extends Object?>({
  1. required TResult invalidShippingField(
    1. InvalidShippingField field,
    2. String? message
    ),
  2. required TResult unserviceableShippingAddress(
    1. String? message
    ),
  3. required TResult invalidCouponCode(
    1. String? message
    ),
  4. required TResult expiredCouponCode(
    1. String? message
    ),
})

A switch-like method, using callbacks.

As opposed to map, this offers destructuring. It is equivalent to doing:

switch (sealedClass) {
  case Subclass(:final field):
    return ...;
  case Subclass2(:final field2):
    return ...;
}

Implementation

@optionalTypeArgs TResult when<TResult extends Object?>({required TResult Function( InvalidShippingField field,  String? message)  invalidShippingField,required TResult Function( String? message)  unserviceableShippingAddress,required TResult Function( String? message)  invalidCouponCode,required TResult Function( String? message)  expiredCouponCode,}) {final _that = this;
switch (_that) {
case _ApplePaySheetErrorInvalidShipping():
return invalidShippingField(_that.field,_that.message);case _ApplePaySheetErrorUnserviceableShipping():
return unserviceableShippingAddress(_that.message);case _ApplePaySheetErrorInvalidCouponCode():
return invalidCouponCode(_that.message);case _ApplePaySheetErrorExpiredCouponCode():
return expiredCouponCode(_that.message);case _:
  throw StateError('Unexpected subclass');

}
}