when<TResult extends Object?> method

  1. @optionalTypeArgs
TResult when<TResult extends Object?>({
  1. required TResult withAccessToken(
    1. AuthToken authToken,
    2. String oldPassword,
    3. String newPassword
    ),
  2. required TResult withFreshAccessToken(
    1. AuthToken freshAuthToken,
    2. String newPassword
    ),
  3. required TResult withEmail(
    1. String email,
    2. String verificationCode,
    3. String newPassword
    ),
  4. required TResult withPhoneNumber(
    1. String phoneNumber,
    2. String verificationCode,
    3. String newPassword
    ),
})

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?>({
  required TResult Function(
          AuthToken authToken, String oldPassword, String newPassword)
      withAccessToken,
  required TResult Function(AuthToken freshAuthToken, String newPassword)
      withFreshAccessToken,
  required TResult Function(
          String email, String verificationCode, String newPassword)
      withEmail,
  required TResult Function(
          String phoneNumber, String verificationCode, String newPassword)
      withPhoneNumber,
}) {
  final _that = this;
  switch (_that) {
    case UpdatePasswordRequestWithAccessToken():
      return withAccessToken(
          _that.authToken, _that.oldPassword, _that.newPassword);
    case UpdatePasswordRequestWithFreshAccessToken():
      return withFreshAccessToken(_that.freshAuthToken, _that.newPassword);
    case UpdatePasswordRequestWithEmail():
      return withEmail(
          _that.email, _that.verificationCode, _that.newPassword);
    case UpdatePasswordRequestWithPhoneNumber():
      return withPhoneNumber(
          _that.phoneNumber, _that.verificationCode, _that.newPassword);
  }
}