onRequest method

  1. @override
Future onRequest(
  1. RequestOptions options,
  2. RequestInterceptorHandler handler
)
override

Called when the request is about to be sent.

Implementation

@override
Future<dynamic> onRequest(
  RequestOptions options,
  RequestInterceptorHandler handler,
) async {
  T? currentToken = await token;
  try {
    if (_shouldRequest != null) {
      currentToken = await _tryRequest(options);
      //just in case token is refreshing
      setToken(currentToken);
    }

    final headers = currentToken != null
        ? _tokenHeader(currentToken)
        : const <String, String>{};
    options.headers.addAll(headers);
    handler.next(options);
  } on DioError catch (e) {
    handler.reject(e);
  }
}