when<TResult extends Object?> method

  1. @optionalTypeArgs
TResult when<TResult extends Object?>(
  1. TResult $default(
    1. String? sha,
    2. String? message,
    3. int commentCount,
    4. int additionsCount,
    5. int deletionsCount,
    6. int totalCount,
    7. ModelUri? url,
    8. ModelUri? htmlUrl,
    9. ModelUri? commentsUrl,
    10. GithubUserModelRef? author,
    11. GithubUserModelRef? committer,
    12. ModelTimestamp? authorDate,
    13. ModelTimestamp? committerDate,
    14. List<GithubCommitModel> parents,
    15. List<GithubContentModel> contents,
    16. bool fromServer,
    )
)

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(
          String? sha,
          String? message,
          int commentCount,
          int additionsCount,
          int deletionsCount,
          int totalCount,
          ModelUri? url,
          ModelUri? htmlUrl,
          ModelUri? commentsUrl,
          @refParam GithubUserModelRef? author,
          @refParam GithubUserModelRef? committer,
          ModelTimestamp? authorDate,
          ModelTimestamp? committerDate,
          @jsonParam List<GithubCommitModel> parents,
          @jsonParam List<GithubContentModel> contents,
          bool fromServer)
      $default,
) {
  final _that = this;
  switch (_that) {
    case _GithubCommitModel():
      return $default(
          _that.sha,
          _that.message,
          _that.commentCount,
          _that.additionsCount,
          _that.deletionsCount,
          _that.totalCount,
          _that.url,
          _that.htmlUrl,
          _that.commentsUrl,
          _that.author,
          _that.committer,
          _that.authorDate,
          _that.committerDate,
          _that.parents,
          _that.contents,
          _that.fromServer);
    case _:
      throw StateError('Unexpected subclass');
  }
}