whenOrNull<TResult extends Object?> method

  1. @optionalTypeArgs
TResult? whenOrNull<TResult extends Object?>(
  1. TResult? $default(
    1. int id,
    2. @JsonKey.new(name: 'isibook_space_id') int isibookSpaceId,
    3. @JsonKey.new(name: 'day_of_week') String dayOfWeek,
    4. @JsonKey.new(name: 'specific_date') String? specificDate,
    5. @JsonKey.new(name: 'start_time') String startTime,
    6. @JsonKey.new(name: 'end_time') String endTime,
    7. bool available,
    8. @JsonKey.new(name: 'created_at') @TimestampConverter() Jiffy createdAt,
    9. @JsonKey.new(name: 'updated_at') @TimestampConverter() Jiffy 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(
          int id,
          @JsonKey(name: 'isibook_space_id') int isibookSpaceId,
          @JsonKey(name: 'day_of_week') String dayOfWeek,
          @JsonKey(name: 'specific_date') String? specificDate,
          @JsonKey(name: 'start_time') String startTime,
          @JsonKey(name: 'end_time') String endTime,
          bool available,
          @JsonKey(name: 'created_at') @TimestampConverter() Jiffy createdAt,
          @JsonKey(name: 'updated_at') @TimestampConverter() Jiffy updatedAt)?
      $default,
) {
  final _that = this;
  switch (_that) {
    case _IsibookSpaceTimeSlot() when $default != null:
      return $default(
          _that.id,
          _that.isibookSpaceId,
          _that.dayOfWeek,
          _that.specificDate,
          _that.startTime,
          _that.endTime,
          _that.available,
          _that.createdAt,
          _that.updatedAt);
    case _:
      return null;
  }
}