when<TResult extends Object?> method

  1. @optionalTypeArgs
TResult when<TResult extends Object?>(
  1. TResult $default(
    1. int? id,
    2. String? name,
    3. String? login,
    4. String? company,
    5. String? blog,
    6. String? location,
    7. String? email,
    8. int publicReposCount,
    9. int publicGistsCount,
    10. int followersCount,
    11. int followingCount,
    12. ModelUri? htmlUrl,
    13. ModelImageUri? avatarUrl,
    14. GithubOrganizationType type,
    15. ModelTimestamp createdAt,
    16. ModelTimestamp updatedAt,
    )
)

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(
          int? id,
          String? name,
          String? login,
          String? company,
          String? blog,
          String? location,
          String? email,
          int publicReposCount,
          int publicGistsCount,
          int followersCount,
          int followingCount,
          ModelUri? htmlUrl,
          ModelImageUri? avatarUrl,
          GithubOrganizationType type,
          ModelTimestamp createdAt,
          ModelTimestamp updatedAt)
      $default,
) {
  final _that = this;
  switch (_that) {
    case _GithubOrganizationModel():
      return $default(
          _that.id,
          _that.name,
          _that.login,
          _that.company,
          _that.blog,
          _that.location,
          _that.email,
          _that.publicReposCount,
          _that.publicGistsCount,
          _that.followersCount,
          _that.followingCount,
          _that.htmlUrl,
          _that.avatarUrl,
          _that.type,
          _that.createdAt,
          _that.updatedAt);
    case _:
      throw StateError('Unexpected subclass');
  }
}