whenOrNull<TResult extends Object?> method

  1. @optionalTypeArgs
TResult? whenOrNull<TResult extends Object?>({
  1. TResult? requestCancelled()?,
  2. TResult? unauthorisedRequest()?,
  3. TResult? unauthorisedRequestApple()?,
  4. TResult? badRequest()?,
  5. TResult? notFound(
    1. String reason
    )?,
  6. TResult? methodNotAllowed()?,
  7. TResult? notAcceptable()?,
  8. TResult? requestTimeout()?,
  9. TResult? sendTimeout()?,
  10. TResult? conflict()?,
  11. TResult? internalServerError()?,
  12. TResult? notImplemented()?,
  13. TResult? serviceUnavailable()?,
  14. TResult? noInternetConnection()?,
  15. TResult? formatException()?,
  16. TResult? unableToProcess()?,
  17. TResult? defaultError(
    1. String error
    )?,
  18. TResult? unexpectedError()?,
  19. TResult? otpExpired()?,
})

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()?  requestCancelled,TResult? Function()?  unauthorisedRequest,TResult? Function()?  unauthorisedRequestApple,TResult? Function()?  badRequest,TResult? Function( String reason)?  notFound,TResult? Function()?  methodNotAllowed,TResult? Function()?  notAcceptable,TResult? Function()?  requestTimeout,TResult? Function()?  sendTimeout,TResult? Function()?  conflict,TResult? Function()?  internalServerError,TResult? Function()?  notImplemented,TResult? Function()?  serviceUnavailable,TResult? Function()?  noInternetConnection,TResult? Function()?  formatException,TResult? Function()?  unableToProcess,TResult? Function( String error)?  defaultError,TResult? Function()?  unexpectedError,TResult? Function()?  otpExpired,}) {final _that = this;
switch (_that) {
case RequestCancelled() when requestCancelled != null:
return requestCancelled();case UnauthorisedRequest() when unauthorisedRequest != null:
return unauthorisedRequest();case UnauthorisedRequestApple() when unauthorisedRequestApple != null:
return unauthorisedRequestApple();case BadRequest() when badRequest != null:
return badRequest();case NotFound() when notFound != null:
return notFound(_that.reason);case MethodNotAllowed() when methodNotAllowed != null:
return methodNotAllowed();case NotAcceptable() when notAcceptable != null:
return notAcceptable();case RequestTimeout() when requestTimeout != null:
return requestTimeout();case SendTimeout() when sendTimeout != null:
return sendTimeout();case Conflict() when conflict != null:
return conflict();case InternalServerError() when internalServerError != null:
return internalServerError();case NotImplemented() when notImplemented != null:
return notImplemented();case ServiceUnavailable() when serviceUnavailable != null:
return serviceUnavailable();case NoInternetConnection() when noInternetConnection != null:
return noInternetConnection();case FormatException() when formatException != null:
return formatException();case UnableToProcess() when unableToProcess != null:
return unableToProcess();case DefaultError() when defaultError != null:
return defaultError(_that.error);case UnexpectedError() when unexpectedError != null:
return unexpectedError();case OtpExpired() when otpExpired != null:
return otpExpired();case _:
  return null;

}
}