getWifiName method

Future<String?> getWifiName()

Returns the current Wi-Fi network name.

Implementation

Future<String?> getWifiName() async {
  if (kIsWeb) {
    return Future.value(null); //not supported on the web
  }
  try {
    if (Platform.isAndroid || Platform.isIOS) {
      String? wifiName;
      if (await getLocationStatus() == LocationStatus.ready.name) {
        wifiName = await _networkInfo.getWifiName();
      } else {
        wifiName = await _networkInfo.getWifiName();
      }
      if (Platform.isAndroid && wifiName != null) {
        //android can put double quotes around wifiname, we need to remove them
        if (wifiName.startsWith('"') && wifiName.endsWith('"')) {
          wifiName = wifiName.substring(1, wifiName.length - 1);
        }
      }
      return wifiName;
    }
  } on PlatformException catch (e) {
    throw PlatformException(
        code: 'Failed to get Wifi name',
        message: e.message,
        details: e.details);
  }
  return null;
}