Reputation: 7750
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());
}
},
Upvotes: 2
Views: 2454
Reputation: 2063
We apply a '.timeout' to update calls as per below. Apply it to the end of your update.
collection.doc(docId)
.update({fieldName: fieldValue})
.timeout(const Duration(seconds: 5),
onTimeout: () => throw CustomTimeoutException);
You can throw a plain Exception or define your own timeout exception, and then just use the catch block as usual. Note that we also use a general connection test across our whole app (as per the other answer) aswell.
Upvotes: 0
Reputation: 27177
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');
}
Upvotes: 2