whenOrNull<TResult extends Object?> method

  1. @optionalTypeArgs
TResult? whenOrNull<TResult extends Object?>(
  1. TResult? $default(
    1. @JsonKey.new(name: 'current_page') int currentPage,
    2. List<IsibookMyReservation> data,
    3. @JsonKey.new(name: 'first_page_url') String? firstPageUrl,
    4. int? from,
    5. @JsonKey.new(name: 'last_page') int? lastPage,
    6. @JsonKey.new(name: 'last_page_url') String? lastPageUrl,
    7. List<IsibookReservationLink> links,
    8. @JsonKey.new(name: 'next_page_url') String? nextPageUrl,
    9. String path,
    10. @JsonKey.new(name: 'per_page') int perPage,
    11. @JsonKey.new(name: 'prev_page_url') String? prevPageUrl,
    12. int? to,
    13. int? total,
    )?
)

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: 'current_page') int currentPage,
          List<IsibookMyReservation> data,
          @JsonKey(name: 'first_page_url') String? firstPageUrl,
          int? from,
          @JsonKey(name: 'last_page') int? lastPage,
          @JsonKey(name: 'last_page_url') String? lastPageUrl,
          List<IsibookReservationLink> links,
          @JsonKey(name: 'next_page_url') String? nextPageUrl,
          String path,
          @JsonKey(name: 'per_page') int perPage,
          @JsonKey(name: 'prev_page_url') String? prevPageUrl,
          int? to,
          int? total)?
      $default,
) {
  final _that = this;
  switch (_that) {
    case _IsibookReservationsPage() when $default != null:
      return $default(
          _that.currentPage,
          _that.data,
          _that.firstPageUrl,
          _that.from,
          _that.lastPage,
          _that.lastPageUrl,
          _that.links,
          _that.nextPageUrl,
          _that.path,
          _that.perPage,
          _that.prevPageUrl,
          _that.to,
          _that.total);
    case _:
      return null;
  }
}