isOnline method

Future<bool> isOnline()

Checks if the device has an active internet connection via pinging cloudFlare's 1.1.1.1

Returns true if online, false if offline or if an error occurs.

Implementation

Future<bool> isOnline() async {
  try {
    // Ping a globally available webpage like CloudFlare's 1.1.1.1
    final response = await http.get(Uri.parse('https://1.1.1.1'));
    return response.statusCode == 200;
  } on SocketException {
    debugPrint('No internet connection.');
    return false;
  } catch (e) {
    debugPrint('Unknown error while checking connectivity: $e');
    return false;
  }
}