Reputation: 317
Is it possible to listen to the entire application in flutter and showing the dialog in case of loss of connection?
Upvotes: 0
Views: 1564
Reputation: 2752
Yes it is possible. And you will need one package for this to work.
You can listen to the stream onConnectivityChanged
from InternetConnectionChecker
.
@override
void initState(){
super.initState();
var isDeviceConnected = false;
var subscription = Connectivity().onConnectivityChanged.listen((ConnectivityResult result) async {
if(result != ConnectivityResult.none) {
isDeviceConnected = await InternetConnectionChecker().hasConnection;
if(!isDeviceConnected){
showDialog(
context,
// Your Dialog Here
);
}
}
});
}
Some things to consider here.
showDialog()
needs a context, so put all of this code on the main
widget with MaterialApp
.pop()
the MaterialApp
itself.Upvotes: 1