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
相关产品推荐
相关产品推荐

