flutter - 参数类型 'void Function(DioError)' 不能分配给参数类型 'void Function(DioError, ErrorInterceptorHandler)?'

标签 flutter dart dio

所以我用一些简单的函数来包装我的 HTTP 请求。喜欢以下:

import 'package:dio/dio.dart';

class HttpService {
  late Dio _dio;

  final String baseUrl = "http://10.0.2.2:8080/community";

  HttpService() {
    _dio = Dio(BaseOptions(
      baseUrl: baseUrl,
    ));

    // initializaInterceptors();
  }

  Future<Response> getRequest(String endPoint) async {
    Response response;
    try {
      response = await _dio.get(endPoint);
    } on DioError catch (e) {
      print(e.message);
      throw Exception(e.message);
    }

    return response;
  }

  Future<Response> postRequest(String endPoint) async {
    Response response;
    try {
      response = await _dio.post(endPoint);
    } on DioError catch (e) {
      print(e.message);
      throw Exception(e.message);
    }

    return response;
  }

  initializaInterceptors() {
      _dio.interceptors.add(InterceptorsWrapper(onError: (error) {
        print(error.message);
      }, onRequest: (request) {
        print("${request.method} ${request.path}");
      }, onResponse: (response) {
        print(response.data);
      }));
    }
  }
}
在过去的几个月里它运行良好。然而,它只是在 initializaInterceptors() 函数中弹出一个错误:

The argument type 'void Function(DioError)' can't be assigned to the parameter type 'void Function(DioError, ErrorInterceptorHandler)?'. The argument type 'void Function(RequestOptions)' can't be assigned to the parameter type 'void Function(RequestOptions, RequestInterceptorHandler)?'. The argument type 'void Function(Response)' can't be assigned to the parameter type 'void Function(Response, ResponseInterceptorHandler)?'.


有谁知道发生了什么?谢谢

最佳答案

initializeInterceptor(){
    _dio.interceptors.add(InterceptorsWrapper(
        onError: (error, errorInterceptorHandler ){
          print(error.message);
        },
        onRequest: (request, requestInterceptorHandler){
          print("${request.method} | ${request.path}");
        },
        onResponse: (response, responseInterceptorHandler) {
          print('${response.statusCode} ${response.statusCode} ${response.data}');
        }
    ));
  }

关于flutter - 参数类型 'void Function(DioError)' 不能分配给参数类型 'void Function(DioError, ErrorInterceptorHandler)?',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66864120/

相关文章:

flutter - paint()方法中的Size属性不同于CustomPainter中指定的大小

Flutter Dart try catch 异常行号

flutter - Flutter中dio的全局配置(拦截器)

ios - iOS 中是否有 INTERNET 权限?

flutter - dio_cookie_manager插件的CookieManager功能与flutter_inappwebview Cookiemanager冲突

flutter - 如何在 json_serializable 中使用私有(private)构造函数

java - Flutter Java android从/app_flutter文件夹中的文件获取数据

dart - 创建从上到下的滑动效果

dart - 如何在Dart中通过CustomEvent测试详细信息的类型?

Dart Streams 取消订阅