when<TResult extends Object?> method

  1. @optionalTypeArgs
TResult when<TResult extends Object?>(
  1. TResult $default(
    1. String id,
    2. String? checkoutUrl,
    3. CartCost? cost,
    4. int? totalQuantity,
    5. List<CartDiscountAllocation?>? discountAllocations,
    6. List<CartDiscountCode?>? discountCodes,
    7. String? createdAt,
    8. List<Attribute?>? attributes,
    9. CartBuyerIdentity? buyerIdentity,
    10. String? note,
    11. String? updatedAt,
    12. List<Line> lines,
    )
)

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?>(TResult Function( String id,  String? checkoutUrl,  CartCost? cost,  int? totalQuantity,  List<CartDiscountAllocation?>? discountAllocations,  List<CartDiscountCode?>? discountCodes,  String? createdAt,  List<Attribute?>? attributes,  CartBuyerIdentity? buyerIdentity,  String? note,  String? updatedAt, @JsonKey(fromJson: JsonHelper.lines)  List<Line> lines)  $default,) {final _that = this;
switch (_that) {
case _Cart():
return $default(_that.id,_that.checkoutUrl,_that.cost,_that.totalQuantity,_that.discountAllocations,_that.discountCodes,_that.createdAt,_that.attributes,_that.buyerIdentity,_that.note,_that.updatedAt,_that.lines);case _:
  throw StateError('Unexpected subclass');

}
}