checkConnectivity method

Future<bool> checkConnectivity()

Checks the internet connectivity by attempting to establish a connection to a specified host

Implementation

Future<bool> checkConnectivity() async {
  Socket? socket;
  bool connectivity;
  await Future.delayed(const Duration(milliseconds: 100));
  try {
    socket = await Socket.connect('google.com', 80,
        timeout: const Duration(seconds: 4));
    connectivity = true;
  } catch (e) {
    checkInternetConnection();
    connectivity = false;
  } finally {
    try {
      await socket?.close();
    } catch (e) {
      // ignore: avoid_print
      print(e);
    }
  }
  return connectivity;
}