RequestUtil constructor

RequestUtil({
  1. required String baseUrl,
  2. required String path,
  3. required RequestMethod method,
  4. required RequestParams? params,
  5. required RequestSuccess success,
  6. required RequestError error,
  7. RequestHeaders? headers,
  8. RequestInterceptors? interceptors,
})

初始化 baseUrl 请求域名/ip path url地址 method 请求方法 params 发起请求的param参数 success 成功回调 error 失败回调 headers 单独设置的请求头 interceptors 自定义拦截器

Implementation

RequestUtil({
  required this.baseUrl,
  required this.path,
  required this.method,
  required this.params,
  required this.success,
  required this.error,
  RequestHeaders? headers,
  RequestInterceptors? interceptors,
}) {
  BaseOptions options = BaseOptions();
  dio = Dio(options);
  // 添加request拦截器
  dio.interceptors.add(RequestInterceptor());
  // 添加response拦截器
  dio.interceptors.add(ResponseInterceptor());
  // 添加error拦截器
  dio.interceptors.add(ErrorInterceptor());
  // 添加logger拦截器
  if (kDebugMode) {
    dio.interceptors.add(LoggerInterceptor());
  }
  // 设置参数
  dio.options = dio.options.copyWith(
    baseUrl: baseUrl,
    connectTimeout: kConnectTimeout,
    receiveTimeout: kReceiveTimeout,
    headers: headers ?? const {},
  );
  if (interceptors != null && interceptors.isNotEmpty) {
    dio.interceptors.addAll(interceptors);
  }
}