Flutter http 异常不会在另一个 try catch 块上抛出
Flutter http exception not throw on another try catch block
我遇到了这个奇怪的异常,它不断出现并冻结我的应用程序。
我正在尝试在 http 中处理 SocketException 和 TimeOutException包含提供商包的包。
class Auth with ChangeNotifier{
..........
Future<User> getUser(String id) async {
try {
final user = (await http.post("${Domain.ADDRESS}/user",
headers: {"auth-token": _token}, body: {"userId": id}))
.body;
} on SocketException {
throw HttpException("DISCONNECTED");
} on TimeoutException {
throw HttpException("TIMEOUT");
} catch (error) {
print(error);
throw HttpException("SOMETHING_WENT_WRONG");
}
notifyListeners();
return userdata;
}
......
}
当未连接应用程序中的互联网冻结在
on SocketException {
throw HttpException("DISCONNECTED"); <------------ Exception has occurred.
HttpException (DISCONNECTED)
但我会在下一个屏幕处理这个问题
@override
Future<void> didChangeDependencies() async {
......
setState(() {
isLoading = true;
});
try{
user= await Provider.of<Auth>(context)
.getUser(Provider.of<Auth>(context).userId);
if (this.mounted) {
setState(() {
isLoading = false;
});
}
}on HttpException catch(error){
if(error.toString().contains("DISCONNECTED")){
Scaffold.of(context).showSnackBar(SnackBar(content: Text("Please check your internet
connection"),));
}
}catch(error){
print(error);
}
super.didChangeDependencies();
}
自定义HttpException.dart
class HttpException implements Exception {
final String message;
HttpException(this.message);
@override
String toString() {
return message;
}
}
我也遇到了这个问题,我发现这是由于我的 flutter beta/dev 版本有问题。
解决方案
我通过将频道更改为稳定版并升级到最新版本来修复它。
flutter channel stable
flutter upgrade
如果你愿意,你也可以把频道换成master来了解flutter的最新特性。但我真的建议使用稳定通道,因为它更稳定,因为最近刚刚发布了一个新的稳定版本。
这里是 link 您遇到的问题:
https://github.com/flutter/flutter/issues/66488
所以如果我理解正确的话,你的 IDE 暂停 当抛出异常时,即使你 捕获 它正确。
你能告诉我恢复/取消暂停你正在使用的 IDE 后会发生什么吗,它是否按预期运行并打印出来?
if(error.toString().contains("DISCONNECTED")){
Scaffold.of(context).showSnackBar(SnackBar(content: Text("Please check your internet
connection"),));
因为如果是,那意味着您可能在 All Exceptions 上设置了 Breakpoints 而不仅仅是 未捕获的异常.
我遇到了这个奇怪的异常,它不断出现并冻结我的应用程序。
我正在尝试在 http 中处理 SocketException 和 TimeOutException包含提供商包的包。
class Auth with ChangeNotifier{
..........
Future<User> getUser(String id) async {
try {
final user = (await http.post("${Domain.ADDRESS}/user",
headers: {"auth-token": _token}, body: {"userId": id}))
.body;
} on SocketException {
throw HttpException("DISCONNECTED");
} on TimeoutException {
throw HttpException("TIMEOUT");
} catch (error) {
print(error);
throw HttpException("SOMETHING_WENT_WRONG");
}
notifyListeners();
return userdata;
}
......
}
当未连接应用程序中的互联网冻结在
on SocketException {
throw HttpException("DISCONNECTED"); <------------ Exception has occurred.
HttpException (DISCONNECTED)
但我会在下一个屏幕处理这个问题
@override
Future<void> didChangeDependencies() async {
......
setState(() {
isLoading = true;
});
try{
user= await Provider.of<Auth>(context)
.getUser(Provider.of<Auth>(context).userId);
if (this.mounted) {
setState(() {
isLoading = false;
});
}
}on HttpException catch(error){
if(error.toString().contains("DISCONNECTED")){
Scaffold.of(context).showSnackBar(SnackBar(content: Text("Please check your internet
connection"),));
}
}catch(error){
print(error);
}
super.didChangeDependencies();
}
自定义HttpException.dart
class HttpException implements Exception {
final String message;
HttpException(this.message);
@override
String toString() {
return message;
}
}
我也遇到了这个问题,我发现这是由于我的 flutter beta/dev 版本有问题。
解决方案
我通过将频道更改为稳定版并升级到最新版本来修复它。
flutter channel stable
flutter upgrade
如果你愿意,你也可以把频道换成master来了解flutter的最新特性。但我真的建议使用稳定通道,因为它更稳定,因为最近刚刚发布了一个新的稳定版本。
这里是 link 您遇到的问题:
https://github.com/flutter/flutter/issues/66488
所以如果我理解正确的话,你的 IDE 暂停 当抛出异常时,即使你 捕获 它正确。
你能告诉我恢复/取消暂停你正在使用的 IDE 后会发生什么吗,它是否按预期运行并打印出来?
if(error.toString().contains("DISCONNECTED")){
Scaffold.of(context).showSnackBar(SnackBar(content: Text("Please check your internet
connection"),));
因为如果是,那意味着您可能在 All Exceptions 上设置了 Breakpoints 而不仅仅是 未捕获的异常.