waitForInternet static method

Future<bool> waitForInternet({
  1. Duration timeout = defaultNetworkWaitTimeout,
})

Wait until the OS reports an attached network, up to timeout.

On a cold start the app runs before the radio/VPN has settled, so a single isConectInternet probe reports offline for a device that is online a moment later. Returns false only when the device is still offline once timeout elapsed.

Implementation

static Future<bool> waitForInternet({
  Duration timeout = defaultNetworkWaitTimeout,
}) async {
  var deadline = DateTime.now().add(timeout);

  while (true) {
    if (await isConectInternet()) return true;
    if (!DateTime.now().isBefore(deadline)) return false;

    await Future.delayed(_networkPollInterval);
  }
}