问题描述
我遇到了这个奇怪的异常,不断出现并冻结我的应用程序。
我尝试使用提供程序包处理 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;
}
......
}
当未连接的应用程序中的Internet冻结在
时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;
}
}
解决方法
因此,如果我理解正确,那么即使引发了异常,您的IDE也会暂停,即使您正确地捕获了。
您能告诉我恢复/取消使用的IDE后会发生什么吗,它的行为是否与预期相同并打印出来?
if(error.toString().contains("DISCONNECTED")){
Scaffold.of(context).showSnackBar(SnackBar(content: Text("Please check your internet
connection"),));
因为这样做,这意味着您可能在所有异常上具有断点设置,而不仅是未捕获的异常。
> ,我也遇到了这个问题,我发现这是由于我拥有错误的beta / dev版本造成的。
解决方案
我通过将频道更改为稳定版并升级到最新版本来修复它。
flutter channel stable
flutter upgrade
如果需要,您还可以将频道更改为主频道,以具有最新的抖动功能。但是我真的建议您使用稳定通道,因为它更稳定并且因为最近刚刚发布了新的稳定版本。
以下是您遇到的问题的链接:
https://github.com/flutter/flutter/issues/66488