Created
January 19, 2023 18:20
-
-
Save TarunNagaSai/e1870512519dce64f3d2cdf6181c404f to your computer and use it in GitHub Desktop.
This file has a getx controller which has connectivity stream Subscription which give out the network status.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'dart:async'; | |
| import 'package:flutter/services.dart'; | |
| import 'package:get/get_state_manager/get_state_manager.dart'; | |
| import 'package:connectivity_plus/connectivity_plus.dart'; | |
| class NetworkServicesController extends GetxController { | |
| bool connectedToNetwork = false; | |
| final Connectivity _connectivity = Connectivity(); | |
| late StreamSubscription<ConnectivityResult> _connectivitySubscription; | |
| @override | |
| void onInit() { | |
| initConnectivity(); | |
| _connectivitySubscription = | |
| _connectivity.onConnectivityChanged.listen(_updateConnectionStatus); | |
| super.onInit(); | |
| } | |
| @override | |
| void dispose() { | |
| _connectivitySubscription.cancel(); | |
| super.dispose(); | |
| } | |
| Future<void> initConnectivity() async { | |
| late ConnectivityResult result; | |
| // Platform messages may fail, so we use a try/catch PlatformException. | |
| try { | |
| result = await _connectivity.checkConnectivity(); | |
| } on PlatformException { | |
| return; | |
| } | |
| return _updateConnectionStatus(result); | |
| } | |
| Future<void> _updateConnectionStatus(ConnectivityResult result) async { | |
| if (result == ConnectivityResult.mobile || | |
| result == ConnectivityResult.wifi) { | |
| connectedToNetwork = true; | |
| } else { | |
| connectedToNetwork = false; | |
| } | |
| update(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment