createInstance static method

void createInstance()

Implementation

static void createInstance() {
  if (dio == null) {
    var options = BaseOptions(
      baseUrl: default_url,
      connectTimeout: const Duration(milliseconds: 30000), // 连接服务器超时时间,单位是毫秒.
      receiveTimeout:
          const Duration(milliseconds: 60000), // 收到数据的最长等待时间,单位是毫秒.
    );
    dio = Dio(options);
    // 添加拦截器
    dio?.interceptors.add(InterceptorsWrapper(onRequest:
        (RequestOptions options, RequestInterceptorHandler handler) {
      // 在请求被发送之前做一些事情
      print(
          'base_plugins:::::http----->请求之前拦截器,url=${options.uri};data=${options.data}');
      return handler.next(options);
    }, onResponse: (Response response, ResponseInterceptorHandler handler) {
      // 在返回响应数据之前做一些预处理
      print(
          'base_plugins:::::http----->请求之后拦截器,url=${response.realUri};data=${response.data}');
      // Do any additional processing here
      handler.next(response);
    }, onError: (DioError e, ErrorInterceptorHandler handler) {
      // 当请求失败时做一些预处理
      handler.next(e);
    }));
    dio?.interceptors.add(
      LogInterceptor(requestBody: true, responseBody: true),
    );
  }
}