watch method

Stream<ConnectivityInfo> watch({
  1. Duration interval = const Duration(seconds: 2),
})

Polls connectivity changes and emits only changed snapshots.

Implementation

Stream<ConnectivityInfo> watch({
  Duration interval = const Duration(seconds: 2),
}) async* {
  if (interval.inMilliseconds <= 0) {
    throw ArgumentError.value(interval, 'interval', 'Must be positive.');
  }

  ConnectivityInfo? previous;
  while (true) {
    final current = await getInfo();
    if (previous == null ||
        current.isConnected != previous.isConnected ||
        current.networkType != previous.networkType ||
        current.isMetered != previous.isMetered ||
        current.isVpn != previous.isVpn) {
      yield current;
      previous = current;
    }
    await Future<void>.delayed(interval);
  }
}