networkLocalIpAddres static method

Future<String?> networkLocalIpAddres()

Implementation

static Future<String?> networkLocalIpAddres() async {
  final interfaces = await NetworkInterface.list(
      type: InternetAddressType.IPv4, includeLinkLocal: true);
  try {
    NetworkInterface? interface = interfaces.singleWhereOrNull((element) =>
        RegExp("^(wl)", caseSensitive: false).hasMatch(element.name));
    if (interface != null) {
      return interface.addresses.first.address;
    }
    try {
      NetworkInterface interface =
          interfaces.firstWhere((element) => element.name == "eth0");
      return interface.addresses.first.address;
    } catch (e) {
      // Try any other connection next
      try {
        NetworkInterface interface = interfaces
            .firstWhere((element) => !(["wlan0"].contains(element.name)));
        return interface.addresses.first.address;
      } catch (ex) {
        return null;
      }
    }
  } catch (ex) {
    try {
      NetworkInterface interface =
          interfaces.firstWhere((element) => element.name == "eth0");
      return interface.addresses.first.address;
    } catch (e) {
      // Try any other connection next
      try {
        NetworkInterface interface = interfaces
            .firstWhere((element) => !(["wlan0"].contains(element.name)));
        return interface.addresses.first.address;
      } catch (ex) {
        return null;
      }
    }
  }
}