Continuously check internet disconnection on Flutter app. You’ve already written the code to do what you want. You could easily wrap it in a page State or an InheritedWidget or some other managerial class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | final Connectivity _connectivity; final StreamSubscription<ConnectivityResult> _subscription; ConstructorForWhateverClassThisIs() { _connectivity = new Connectivity(); _subscription = _connectivity.onConnectivityChanged.listen(onConnectivityChange); } void onConnectivityChange(ConnectivityResult result) { // TODO: Show snackbar, etc if no connectivity } void dispose() { // Always remember to cancel your subscriptions when you're done. subscription.cancel(); } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.