onConnectivityChanged property
Subscribe to this stream to receive events whenever the
ConnectionStatus changes. When a listener is attached
a check is performed immediately and the status (ConnectionStatus)
is emitted. After that a timer starts which performs
checks with the specified interval - checkInterval.
Default is default_Interval.
As long as there's an attached listener, checks are being performed, so remember to dispose of the subscriptions when they're no longer needed.
Example:
var listener = ConnectionChecker().onConnectivityChanged.listen((status) {
switch(status) {
case ConnectionStatus.connected:
print('Data connection is available.');
break;
case ConnectionStatus.disconnected:
print('You are disconnected from the internet.');
break;
}
});
Note: Remember to dispose of any listeners,
when they're not needed anymore,
e.g. in a StatefulWidget's dispose() method
...
@override
void dispose() {
listener.cancel();
super.dispose();
}
...
For as long as there's an attached listener, requests are
being made with an interval of checkInterval. The timer stops
when an automatic check is currently executed, so this interval
is a bit longer actually (the maximum would be checkInterval +
the maximum timeout for an address in addresses). This is by design
to prevent multiple automatic calls to connectionStatus, which
would wreck havoc.
You can, of course, override this behavior by implementing your own
variation of time-based checks and calling either connectionStatus
or hasConnection as many times as you want.
When all the listeners are removed from onConnectivityChanged, the internal
timer is cancelled and the stream does not emit events.
Implementation
Stream<ConnectionStatus> get onConnectivityChanged =>
_statusController.stream;