没有或没有网络连接时,应用程序会崩溃 [英] flutter app crash when there is low or no network connection
本文介绍了没有或没有网络连接时,应用程序会崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个IconButton,当我按下它时会更新我的Cloud Firestore DB上的一个字段。如果互联网连接正常,没有网络或信号不足,我都不会超时错误,这会导致应用崩溃。我已经将函数包装在try {} catch {}块中,但仍然无济于事。
I have this IconButton which when I press it updates a field on my cloud Firestore DB.I do not get any problem if there is a healthy internet connection, how ever is there is no network or low signal I get a timed out error and this causes the app to crash. I have have wraped the function in a try{} catch{} block but still that doesn't help.
onPressed: (){
try{
Firestore.instance.runTransaction((Transaction thistransaction)async{
DocumentSnapshot docSnapshot = await thistransaction
.get(snapshotDocuments[index].reference);
await thistransaction.update(docSnapshot.reference,
{'voteUpBool':!docSnapshot['voteUpBool']});
});
}
catch(err){
print(err.toString());
}
},
推荐答案
您必须检查Internet或wifi是否可用,只需添加最新版本的 connectivity:^ 0.3.1 依赖项并遵循以下代码即可。
you have to check internet or wifi available or not for that just add connectivity: ^0.3.1 dependency with latest version and follow the below code.
import 'dart:io';
...
try {
final result = await InternetAddress.lookup('google.com');
if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
print('connected');
}
} on SocketException catch (_) {
print('not connected');
}
这篇关于没有或没有网络连接时,应用程序会崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文