hasInternet static method

Future<bool> hasInternet({
  1. bool force = false,
  2. Duration timeout = const Duration(seconds: 5),
})

Whether the internet (by default: your own API) actually answers. Any HTTP response counts, since a 401 still proves the round trip works. Cached for probeCacheDuration.

Implementation

static Future<bool> hasInternet({bool force = false, Duration timeout = const Duration(seconds: 5)}) {
  if (!status.connected) return Future<bool>.value(false);
  final DateTime? at = _probeAt;
  if (!force && at != null && _probeResult != null && DateTime.now().difference(at) < probeCacheDuration) return Future<bool>.value(_probeResult);
  return _probing ??= _probe(timeout)
      .then((bool ok) {
        _probeResult = ok;
        _probeAt = DateTime.now();
        return ok;
      })
      .whenComplete(() => _probing = null);
}