Skip to content

Instantly share code, notes, and snippets.

@berkanaslan
Last active May 31, 2021 17:25
Show Gist options
  • Save berkanaslan/1e8506886f3bf9b999d7704da09ac92b to your computer and use it in GitHub Desktop.
Save berkanaslan/1e8506886f3bf9b999d7704da09ac92b to your computer and use it in GitHub Desktop.
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