You could use a timout like this:
if (await database
.reference()
.child("my/child/node")
.once()
.timeout(Duration(seconds: 5), onTimeout: () {
print('Spent 5 seconds and no connection');
}).isNotEmpty) {
print('Connected successfully');
Map map = d.value;
String val1 = map["val1"];
String val2 = map["val2"];
String val3 = map["val3"];
}
But you should test it first because i didn't test isNotEmpty function in that case, you should test it and see what dose the line code
await database
.reference()
.child("my/child/node")
.once()
gives you on successful connection and make the "if" condition as it should.
i.e:
if (await http.get("google.com")
.timeout(Duration(seconds: 5), onTimeout: () {
print('Spent 5 seconds and no connection');
})==200) {
print('Connected successfully');}
Or you can check for an internet connection before calling the database line.
You could use something like this.
import 'dart:io';
try {
final result = await InternetAddress.lookup('google.com');
if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
DataSnapshot d = await database.reference().child("my/child/node").once();
Map map = d.value;
String val1 = map["val1"];
String val2 = map["val2"];
String val3 = map["val3"]; }
} on SocketException catch (_) {
print('not connected');
}