Flutter 颤振:Dio HTTP处理错误异常404 我的期望
我在处理错误异常DIO程序包时遇到问题,我正在HTTP程序包中尝试此代码:Flutter 颤振:Dio HTTP处理错误异常404 我的期望,flutter,dart,Flutter,Dart,我在处理错误异常DIO程序包时遇到问题,我正在HTTP程序包中尝试此代码: Future<List<Mosque>> getMahasiswaById(String id) async{ try { var apiRespon = await http.get('$baseURL/mahasiswa/get/id/$id'); if(apiRespon.statusCode == 200){ fin
Future<List<Mosque>> getMahasiswaById(String id) async{
try {
var apiRespon = await http.get('$baseURL/mahasiswa/get/id/$id');
if(apiRespon.statusCode == 200){
final apiResponJson = json.decode(apiRespon.body);
return (apiResponJson['data'] as List).map((p)=>Mosque.fromJson(p)).toList();
}else{
print(apiRespon.statusCode.toString());
throw Exception('Failed Load Data with status code ${apiRespon.statusCode}');
}
}on Exception catch (e) {
print(e);
return null;
}
}
Future getMahasiswaById(字符串id)异步{
试一试{
var apiRespon=wait http.get(“$baseURL/mahasiswa/get/id/$id”);
如果(apiRespon.statusCode==200){
final apiResponJson=json.decode(apiRespon.body);
return(apireponjson['data']as List).map((p)=>Mosque.fromJson(p)).toList();
}否则{
打印(apiRespon.statusCode.toString());
抛出异常(“加载数据失败,状态代码为${apiRespon.statusCode}”);
}
}关于异常捕获(e){
印刷品(e);
返回null;
}
}
但是,如果我使用DIO进行更改,我的应用程序会突然崩溃并出现错误异常,即使我已经在处理此异常:
Future<List<Mosque>> getMahasiswaById(String id) async{
try {
var apiRespon = await dio.get('$baseURL/mahasiswa/get/id/$id');
if(apiRespon.statusCode == 200){
final apiResponJson = apiRespon.data;
return (apiResponJson['data'] as List).map((p)=>Mosque.fromJson(p)).toList();
}else{
print(apiRespon.statusCode.toString());
throw Exception('Failed Load Data with status code ${apiRespon.statusCode}');
}
}on DioError catch (e) {
print(e);
return null;
}
}
Future getMahasiswaById(字符串id)异步{
试一试{
var apiRespon=wait dio.get(“$baseURL/mahasiswa/get/id/$id”);
如果(apiRespon.statusCode==200){
最终apiResponJson=apiRespon.data;
return(apireponjson['data']as List).map((p)=>Mosque.fromJson(p)).toList();
}否则{
打印(apiRespon.statusCode.toString());
抛出异常(“加载数据失败,状态代码为${apiRespon.statusCode}”);
}
}关于错误捕获(e){
印刷品(e);
返回null;
}
}
我的例外:
dio.post().then(){ }.catchError(){ }
错误:null消息:“Http状态错误[404]”
响应({“状态”:false,“消息”:“未找到数据”}) 我已经在github中打开了这个问题,但还没有得到解决方案 谢谢
在您的第一个代码中,使用http包,如果状态代码不是200,您将引发一个被捕获的
异常
on Exception catch (e) { ... }
但是,在使用DIO包的第二个代码中,您引发了相同的异常
,但在更改捕获条件时无法捕获该异常
on DioError catch (e) { ... }
由于异常不是由此引发的,因此它会通过您的应用程序传播。您可以使用.catchError()
捕获异常:
dio.post().then(){ }.catchError(){ }
而try catch
无效。
查看更多信息:您可以使用.catchError()在状态代码200时捕获异常
Future<void> _postRequest() async {
...
await RequestUtil().post(path, data: data)
.then(_handleDioResponse)
.catchError(_handleDioError);
}
void _handleDioResponse(dynamic response){
...
}
void _handleDioError(dynamic error){
...
}
Future\u postRequest()异步{
...
wait RequestUtil().post(路径,数据:数据)
.然后(_handleDioResponse)
.catchError(_handlediaError);
}
void\u handleDioResponse(动态响应){
...
}
void\u handleDiaError(动态错误){
...
}
您可以发布引发的异常的详细信息吗?为什么删除了json.decode
?另外,您发布的iamge(您应该发布文本)是404错误,这意味着url有问题,没有返回任何内容@DIO数据中的F-1已经映射了,这就是我删除的json。解码,如果我使用的是http。get
@Muldec,这是不同的。对不起,它已经在链接图像的描述中了,先生。确定它在图像上。但是,你能把它作为一个文本发布,以便将来参考和更好地索引你的问题吗?对不起,什么是最好的解决方案?我正在删除if else
语句中的异常?我不能给你这个问题的答案。这取决于您希望如何管理应用程序中的异常以及如何管理http状态代码。在这一点上,我希望忽略异常错误并使我的应用程序继续运行而不崩溃,因为我知道它肯定404未找到
,然后重新生成行throw Exception('Failed Load Data with status code${apiRespon.statusCode}');
lol这确实有效,但它被否决了。。。