whenOrNull<TResult extends Object?> method

  1. @optionalTypeArgs
TResult? whenOrNull<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. @refParam GithubUserModelRef? author,
    11. @refParam GithubUserModelRef? committer,
    12. ModelTimestamp? authorDate,
    13. ModelTimestamp? committerDate,
    14. @jsonParam List<GithubCommitModel> parents,
    15. @jsonParam List<GithubContentModel> contents,
    16. bool fromServer,
    )?
)

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(
          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() when $default != null:
      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 _:
      return null;
  }
}