hasConnection static method

Future<bool> hasConnection({
  1. String lookUpUrl = "google.com",
  2. bool lookUp = false,
})

Implementation

static Future<bool> hasConnection({String lookUpUrl = "google.com", bool lookUp = false}) async{
  final targetUrl = lookUpUrl;
  final connectivity = await checkConnectivity();
  if(connectivity == ConnectivityResult.none){
    //There is no network found, so there will not be any network connection
    logNUI(_MODULE, "No connection found from either mobile or wifi");
    return false;
  }

  //Device is connected to either a mobile network or a wifi network, re-confirm network connection by connecting to target url
  try {
    if(lookUp == true) {
      logNUI(_MODULE, "Proceed to look up internet connection");
      final result = await InternetAddress.lookup(targetUrl);
      if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
        return true;
      }
    }
    else {
      return true;
    }
  } catch (e) {
    logNUI(_MODULE, "Failed to connect to $targetUrl with error : $e");
  }
  return false;
}