所以我用一些简单的函数来包装我的 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/