whenOrNull<TResult extends Object?> method

  1. @optionalTypeArgs
TResult? whenOrNull<TResult extends Object?>(
  1. TResult? $default(
    1. int id,
    2. int accountId,
    3. String address,
    4. String city,
    5. String zipCode,
    6. String province,
    7. String country,
    8. String? taxCode,
    9. String? vatNumber,
    10. String? businessName,
    11. DateTime? deletedAt,
    12. DateTime? createdAt,
    13. DateTime? updatedAt,
    )?
)

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(
          @JsonKey(name: 'id') int id,
          @JsonKey(name: 'account_id') int accountId,
          @JsonKey(name: 'address') String address,
          @JsonKey(name: 'city') String city,
          @JsonKey(name: 'zip_code') String zipCode,
          @JsonKey(name: 'province') String province,
          @JsonKey(name: 'country') String country,
          @JsonKey(name: 'tax_code') String? taxCode,
          @JsonKey(name: 'vat_number') String? vatNumber,
          @JsonKey(name: 'business_name') String? businessName,
          @JsonKey(name: 'deleted_at') DateTime? deletedAt,
          @JsonKey(name: 'created_at') DateTime? createdAt,
          @JsonKey(name: 'updated_at') DateTime? updatedAt)?
      $default,
) {
  final _that = this;
  switch (_that) {
    case _AccountAddress() when $default != null:
      return $default(
          _that.id,
          _that.accountId,
          _that.address,
          _that.city,
          _that.zipCode,
          _that.province,
          _that.country,
          _that.taxCode,
          _that.vatNumber,
          _that.businessName,
          _that.deletedAt,
          _that.createdAt,
          _that.updatedAt);
    case _:
      return null;
  }
}