isConnected method

Future<bool> isConnected()

Check the internet connection status. Returns true if connected, false otherwise.

Implementation

Future<bool> isConnected() async {
  try {
    // Get the current connectivity status.
    final result = await _connectivity.checkConnectivity();

    // Return true if the connection is not 'none' (i.e., there is internet).
    if (result.any((element) => element != ConnectivityResult.none)) {
      return true;
    } else {
      return false;
    }
  } on PlatformException catch (_) {
    // Return false if there is an exception (e.g., platform-specific issue).
    return false;
  }
}