when<TResult extends Object?> method

  1. @optionalTypeArgs
TResult when<TResult extends Object?>(
  1. TResult $default(
    1. int id,
    2. @JsonKey.new(name: 'commercial_local_id') String commercialLocalId,
    3. String name,
    4. @JsonKey.new(name: 'description_discount') String? descriptionDiscount,
    5. num price,
    6. int points,
    7. String? note,
    8. @JsonKey.new(name: 'created_at') @TimestampConverter() Jiffy? createdAt,
    9. @JsonKey.new(name: 'updated_at') @TimestampConverter() Jiffy? updatedAt,
    )
)

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(
          int id,
          @JsonKey(name: 'commercial_local_id') String commercialLocalId,
          String name,
          @JsonKey(name: 'description_discount') String? descriptionDiscount,
          num price,
          int points,
          String? note,
          @JsonKey(name: 'created_at') @TimestampConverter() Jiffy? createdAt,
          @JsonKey(name: 'updated_at') @TimestampConverter() Jiffy? updatedAt)
      $default,
) {
  final _that = this;
  switch (_that) {
    case _CommercialPointPack():
      return $default(
          _that.id,
          _that.commercialLocalId,
          _that.name,
          _that.descriptionDiscount,
          _that.price,
          _that.points,
          _that.note,
          _that.createdAt,
          _that.updatedAt);
  }
}