whenOrNull<TResult extends Object?> method

  1. @optionalTypeArgs
TResult? whenOrNull<TResult extends Object?>(
  1. TResult? $default(
    1. String? cartId,
    2. String? intent,
    3. PPBillingType? billingType,
    4. String? paymentId,
    5. String? billingToken,
    6. List<PPItem>? items,
    7. CartAmounts? amounts,
    8. String? description,
    9. @JsonKey.new(name: 'cancelUrl', readValue: _PPCartHelper.readUrl) String? cancelUrl,
    10. @JsonKey.new(name: 'returnUrl', readValue: _PPCartHelper.readUrl) String? returnUrl,
    11. PPAmount? total,
    12. List<PPShippingMethods>? shippingMethods,
    13. PPCartAddress? shippingAddress,
    14. PPCartAddress? billingAddress,
    15. PPAmount? totalAllowedOverCaptureAmount,
    )?
)

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(
          String? cartId,
          String? intent,
          PPBillingType? billingType,
          String? paymentId,
          String? billingToken,
          List<PPItem>? items,
          CartAmounts? amounts,
          String? description,
          @JsonKey(name: 'cancelUrl', readValue: _PPCartHelper.readUrl)
          String? cancelUrl,
          @JsonKey(name: 'returnUrl', readValue: _PPCartHelper.readUrl)
          String? returnUrl,
          PPAmount? total,
          List<PPShippingMethods>? shippingMethods,
          PPCartAddress? shippingAddress,
          PPCartAddress? billingAddress,
          PPAmount? totalAllowedOverCaptureAmount)?
      $default,
) {
  final _that = this;
  switch (_that) {
    case _PPCart() when $default != null:
      return $default(
          _that.cartId,
          _that.intent,
          _that.billingType,
          _that.paymentId,
          _that.billingToken,
          _that.items,
          _that.amounts,
          _that.description,
          _that.cancelUrl,
          _that.returnUrl,
          _that.total,
          _that.shippingMethods,
          _that.shippingAddress,
          _that.billingAddress,
          _that.totalAllowedOverCaptureAmount);
    case _:
      return null;
  }
}