onRequest method

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

Called when the request is about to be sent.

Implementation

@override
void onRequest(
    dio.RequestOptions options, dio.RequestInterceptorHandler handler) {
  // Decided once per attempt and always overwritten, so a mid-flight options
  // change or a retry reusing these options cannot act on a stale decision.
  final globalOptions = CxFlutterPlugin.globalOptions;
  final networkOff = globalOptions != null &&
      !globalOptions.isInstrumentationEnabled(CXInstrumentationType.network);
  options.extra[_kNetworkOffKey] = networkOff;
  _clearInjectedTraceContext(options);
  if (networkOff) {
    handler.next(options);
    return;
  }

  // Dio cannot see an async throw from here, so the request is forwarded
  // whatever happens while preparing it.
  _addTraceContext(options, globalOptions).catchError((Object e) {
    debugPrint('[CxDioInterceptor] onRequest failed: $e');
  }).whenComplete(() {
    // Monotonic clock, started as the request leaves so the reported
    // duration is the request's own. Wall time can jump (NTP sync,
    // timezone change) mid-request.
    options.extra[_kStopwatchKey] = Stopwatch()..start();
    handler.next(options);
  });
}