无法捕获flutter中dio包的错误

问题描述 投票:0回答:2

我正在使用 dio 包实现 post 方法,当我尝试捕获 dioError 中的异常时,它不会捕获任何错误,我使用拦截器,我发现应用程序永远不会调用 OnError 方法。

我搜索了不同的文章和帖子,但没有具体提到这个问题,我使用 dio 版本 4.0.2 仍然有同样的问题,我尝试扩展异常文件中的 DioError 但什么也没发生

发帖方式:

@override
  Future post(
    String path, {
    Map<String, dynamic>? body,
    bool formDataIsEnabled = false,
    Map<String, dynamic>? queryParams,
  }) async {
    try {
      debugPrint("ENTERING THE POST METHOD:");
      final response = await client.post(path,
          queryParameters: queryParams,
          data: formDataIsEnabled ? FormData.fromMap(body!) : body);
      return _handleJsonResponse(response);
    } on DioError catch (error) {
        return _handleDioError(error);
    }
  }

处理方法:

 dynamic _handleJsonResponse(Response<dynamic> response) {
    final jsonResponse = jsonDecode(response.data);
    return jsonResponse;
  }

  dynamic _handleDioError(DioError error) {
    switch (error.type) {
      case DioErrorType.connectTimeout:
        break;
      case DioErrorType.sendTimeout:
        break;
      case DioErrorType.receiveTimeout:
        throw FetchDataException();
      case DioErrorType.response:
        switch (error.response?.statusCode) {
          case StatusCode.badRequest:
            throw BadRequestException();
          case StatusCode.unauthorized:
          case StatusCode.forbidden:
            throw UnauthorisedException();
          case StatusCode.notFound:
            throw NotFoundException();
          case StatusCode.conflict:
            throw ConflictException();
          case StatusCode.internalServerError:
            throw InternalServerErrorException();
        }
        break;
      case DioErrorType.cancel:
        break;
      case DioErrorType.other:
        throw ServerExceptions(message: "Error");
    }
  }

拦截器:

class AppInterceptors extends Interceptor {
  @override
  Future onRequest(RequestOptions options, RequestInterceptorHandler handler) async{
    debugPrint('REQUEST [${options.method}]=>PATH: ${options.path}');
    options.headers[AppStrings.contentType] = AppStrings.applicationContentType;
    super.onRequest(options, handler);
  }

  @override
  Future onResponse(Response response, ResponseInterceptorHandler handler)async {
    debugPrint('RESPONSE[${response.statusCode}] => PATH: ${response.requestOptions.path}');
     super.onResponse(response, handler);
  }
  @override
  Future onError(DioError err, ErrorInterceptorHandler handler) async{
    debugPrint('ERROR[${err.response?.statusCode}] => PATH: ${err.requestOptions.path}');
   super.onError(err, handler);
  }

flutter api dart interceptor dio
2个回答
1
投票

尝试在发布请求后检查响应状态。有时 API 不会生成异常,因此您需要添加对响应的检查

statusCode


0
投票

来自服务器的所有响应都必须包含状态代码,默认情况下,如果状态代码在 2xx (200 - 299) 范围内,Dio 会将响应视为成功,其他都是错误。

因此,每个响应都会调用

onResponse
onError
,具体取决于其状态代码。

检查

ValidateStatus
,位于
BaseOptions

例如,如果您想要未经授权的响应(状态代码为 401),将调用

onError
,像这样的配置

如果

onError
方法从未被调用,也许你的配置方法
validateStatus
在所有情况下总是返回 true。

© www.soinside.com 2019 - 2024. All rights reserved.