getDeviceIpAddress function

Future<String?> getDeviceIpAddress()

Implementation

Future<String?> getDeviceIpAddress() async {
  try {
    final connectivityResult = await (Connectivity().checkConnectivity());
    String? currentIpAddress;

    if (connectivityResult.any((element) =>
        element == ConnectivityResult.wifi ||
        element == ConnectivityResult.ethernet)) {
      currentIpAddress = await getIPAddress();
    }

    if (connectivityResult.any((element) =>
        element == ConnectivityResult.mobile ||
        element == ConnectivityResult.vpn)) {
      for (var interface in await NetworkInterface.list()) {
        if (interface.name == 'wlan1') {
          currentIpAddress = interface.addresses.first.address;
        }
      }
    }
    return currentIpAddress;
  } catch (e) {
    return null;
  }
}