Last active
May 31, 2021 17:25
-
-
Save berkanaslan/1e8506886f3bf9b999d7704da09ac92b to your computer and use it in GitHub Desktop.
This file contains 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
class ConnectivityViewModel with ChangeNotifier { | |
// ------------------------------------- | |
// Variables | |
// ------------------------------------- | |
ConnectivityResult _status; | |
ConnectivityResult get status => _status; | |
set status(ConnectivityResult status) { | |
_status = status; | |
notifyListeners(); | |
} | |
Stream<ConnectivityResult> connectivity = Connectivity().onConnectivityChanged; | |
// ------------------------------------- | |
// Functions | |
// ------------------------------------- | |
void getInitialConnectivityStatus() async { | |
status = await Connectivity().checkConnectivity(); | |
} | |
void onConnectivityChanged() { | |
connectivity.listen((ConnectivityResult result) { | |
print("Connectivity status changed! Current status: $result"); | |
status = result; | |
return result; | |
}); | |
} | |
// ------------------------------------- | |
// Constructor | |
// ------------------------------------- | |
ConnectivityViewModel() { | |
getInitialConnectivityStatus(); | |
onConnectivityChanged(); | |
print("Initialized: ${this.runtimeType}"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment