createDio method

Dio createDio({
  1. bool shouldCopyBaseOptions = true,
  2. bool shouldAddLogger = true,
  3. bool shouldAddRetry = true,
  4. bool shouldAddCache = true,
  5. bool shouldAddInterceptors = true,
})

创建dio通用方法

Implementation

Dio createDio(
    {bool shouldCopyBaseOptions = true,
    bool shouldAddLogger = true,
    bool shouldAddRetry = true,
    bool shouldAddCache = true,
    bool shouldAddInterceptors = true}) {
  Dio dio;

  if (shouldCopyBaseOptions) {
    dio = Dio(copyBaseOptions());
  } else {
    dio = Dio();
  }

  if (openLog && _isInDebugMode && shouldAddLogger) {
    dio.interceptors.add(TalkerDioLogger(
      settings: _talkerDioLoggerSettings,
    ));
  }

  if (retry != 0 && shouldAddRetry) {
    dio.interceptors.add(RetryInterceptor(
      dio: dio,
      logPrint: print, // specify log function (optional)
      retries: retry, // retry count (optional)
      retryDelays: const [
        // set delays between retries (optional)
        Duration(seconds: 1), // wait 1 sec before first retry
        Duration(seconds: 2), // wait 2 sec before second retry
        Duration(seconds: 3), // wait 3 sec before third retry
      ],
    ));
  }

  if (_cacheOptions != null && shouldAddCache) {
    dio.interceptors.add(DioCacheInterceptor(options: _cacheOptions!));
  }

  if (_interceptors != null && shouldAddInterceptors) {
    dio.interceptors.addAll(_interceptors!);
  }

  return dio;
}