所以我用一些简单的函数来包装我的 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() 函数中弹出一个错误:
无法将参数类型“void Function(DioError)”分配给参数类型“void Function(DioError, ErrorInterceptorHandler)?”。 不能将参数类型“void Function(RequestOptions)”分配给参数类型“void Function(RequestOptions, RequestInterceptorHandler)?”。 无法将参数类型“void Function(Response)”分配给参数类型“void Function(Response, ResponseInterceptorHandler)?”。
有人知道这是怎么回事吗?谢谢
答案 0 :(得分:5)
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}');
}
));
}