【发布时间】:2019-07-23 09:29:15
【问题描述】:
我是 dart 编程的新手,所以我尝试检查嵌套在 FutureBuilder 中的 switch case 中的异常子类型,但我找不到令人满意的解决方案...
我试图检查 switch-case 但它不起作用但是当我尝试使用 is 的 if-else 时它正在工作......
我的自定义异常子类型:
class HttpException implements Exception {
HttpStatusError status;
String message;
HttpException(int statusCode) {
switch (statusCode) {
case 400:
this.status = HttpStatusError.BadRequest;
this.message = "Bad request";
break;
case 401:
this.status = HttpStatusError.UnAuthorized;
this.message = "UnAuthorized access ";
break;
case 403:
this.status = HttpStatusError.Forbidden;
this.message = "Resource access forbidden";
break;
case 404:
this.status = HttpStatusError.NotFound;
this.message = "Resource not Found";
break;
case 500:
this.status = HttpStatusError.InternalServerError;
this.message = "Internal server error";
break;
default:
this.status = HttpStatusError.Unknown;
this.message = "Unknown";
break;
}
}
enum HttpStatusError {
UnAuthorized,
BadRequest,
Forbidden,
NotFound,
InternalServerError,
Unknown
}
if (snapshot.hasError) {
final error = snapshot.error;
print(error is HttpException);
switch (error) {
case HttpException:
return Text("http exception";
case SocketException:
return Center(child: Text("socket exception"));
}
return Center(child: Text("Error occured ${snapshot.error}"));
}
打印指令:print(error is HttpException); 显示true 值,但我没有在SocketException 的情况下输入。
【问题讨论】: