i have the following simple write to database real time
FirebaseDatabase.instance.ref().child('A').update({
'name': 'my name',
});
I noticed if I did this writing process in case there is no connection, it will wait the connection to be available again then it will write .. but How can i prevent it
in other word How can i make this process write only be Successful if user has connection to the internet and ignore any pending process write even if they were writing in offline mode
i need it for only specific method and not all
CodePudding user response:
If you just want to fire the write event when you have an internet connection, you can achieve this is by checking your internet connection before you call the function using internet_connection_checker package https://pub.dev/packages/internet_connection_checker
import 'package:connectivity_plus/connectivity_plus.dart';
bool hasConnection = await InternetConnectionChecker().hasConnection;
if (hasConnection) {
// This will only fire if you are connected to the internet
FirebaseDatabase.instance.ref().child('A').update({
'name': 'my name',
});
}
CodePudding user response:
You can easily wrap this in a Future.value like this :
var future = Future.value(FirebaseDatabase.instance.ref().child('A').update({'name': 'my name',}););
future.then((value) => print(value));
One can imagine that you will setState in the then which will update the UI.