onStatusChange property
Subscribe to this stream to receive events whenever the DataConnectionStatus changes. When a listener is attached a check is performed immediately and the status (DataConnectionStatus) 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 = DataConnectionChecker().onStatusChange.listen((status) {
switch(status) {
case DataConnectionStatus.connected:
print('Data connection is available.');
break;
case DataConnectionStatus.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 onStatusChange
, the internal
timer is cancelled and the stream does not emit events.
Implementation
Stream<DataConnectionStatus> get onStatusChange => _statusController.stream;