You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter Dio包^5.4.0版本DioExceptionType常量缺失问题求助

解决dio ^5.4.0中DioExceptionType枚举成员缺失问题

在dio的^5.4.0版本中,DioExceptionType枚举做了版本更新,原有的response和other成员已被移除,对应替换为新的枚举值,以下是具体修正方案:

错误原因与对应替换

  • 原DioExceptionType.response → 替换为DioExceptionType.badResponse:用于捕获服务器返回非2xx状态码的异常
  • 原DioExceptionType.other → 替换为DioExceptionType.unknown:用于处理其他未明确归类的异常情况

修正后的完整代码

factory ServerFailure.fromDioError(DioException dioError){
    switch (dioError.type) {
      case DioExceptionType.connectionTimeout:
        return ServerFailure("Connection timeout with ApiServer");
      case DioExceptionType.sendTimeout:
        return ServerFailure("Send timeout with ApiServer");

      case DioExceptionType.receiveTimeout:
        return ServerFailure("Receive timeout with ApiServer");
      case DioExceptionType.badResponse:
        return ServerFailure.fromResponse(
            dioError.response!.statusCode!, dioError.response!.data);
      case DioExceptionType.cancel:
        return ServerFailure("Request to ApiServer was canceled");
      case DioExceptionType.unknown:
        if(dioError.message!.contains('SocketException')){
          return ServerFailure("No Internet Connection");
        }
        return ServerFailure("Unexpected Error, Please try again");
      default:
        return ServerFailure('Oops There was an Error, Please try again');
    }
  }

顺带修正了代码里的拼写错误:connectoin→Connection、Internel→Internet、Opps→Oops,避免不必要的语义问题。

内容的提问来源于stack exchange,提问作者adham allan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 21:16:16