扑应用程序崩溃时,有低或无网络连接(flutter app crash when there is

2019-10-28 07:58发布

我有这个IconButton,当我按下它更新我的云公司的FireStore DB.I一个领域,如果有一个健康的互联网连接,没有得到任何的问题,有没有是怎么过没有网络或低信号,我收到了超时错误,此导致应用程序崩溃。 我已经都挤包的功能在一个try {}赶上{}块,但仍然没有帮助。

 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());
                  }
                 },

Answer 1:

你必须检查网络或无线网络提供或不只是添加连接:^ 0.3.1依赖与最新版本,并按照下面的代码。

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');
 }


文章来源: flutter app crash when there is low or no network connection