How to handle the internet connection in flutter?

First, add connectivity plugin in puspec.yaml

Then declare some variable under State<>{
var _connectionStatus = 'Unknown';
Connectivity connectivity;
StreamSubscription<ConnectivityResult> subscription;



void initState(){
super.initState();
connectivity = new Connectivity();
subscription = connectivity.onConnectivityChanged.listen((ConnectivityResult result){
_connectionStatus = result.toString()
print(_connectionStatus);
if(result == ConnectivityResult.wifi || result == ConnectivityResult.mobile){

//call our method
}


});
}


void disposer(){
subscription.cancel();
super.dispose();
}

Comments