when<TResult extends Object?> method

  1. @optionalTypeArgs
TResult when<TResult extends Object?>(
  1. TResult $default(
    1. ChatCompletionListObject object,
    2. List<CreateChatCompletionResponse> data,
    3. String? firstId,
    4. String? lastId,
    5. bool hasMore,
    )
)

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(
          ChatCompletionListObject object,
          List<CreateChatCompletionResponse> data,
          @JsonKey(name: 'first_id', includeIfNull: false) String? firstId,
          @JsonKey(name: 'last_id', includeIfNull: false) String? lastId,
          @JsonKey(name: 'has_more') bool hasMore)
      $default,
) {
  final _that = this;
  switch (_that) {
    case _ChatCompletionList():
      return $default(_that.object, _that.data, _that.firstId, _that.lastId,
          _that.hasMore);
    case _:
      throw StateError('Unexpected subclass');
  }
}