getLocalIp static method

Future<String> getLocalIp()

Get local private IP address Returns the first non-loopback IPv4 address found Throws ShspNetworkException if no suitable address is found

Implementation

static Future<String> getLocalIp() async {
  try {
    final interfaces = await NetworkInterface.list();

    for (final interface in interfaces) {
      for (final addr in interface.addresses) {
        if (addr.type == InternetAddressType.IPv4 &&
            !addr.isLoopback &&
            _isPrivateIp(addr.address)) {
          return addr.address;
        }
      }
    }

    throw ShspNetworkException(
      'Unable to determine local private IP address - no suitable network interface found',
    );
  } on SocketException catch (e) {
    throw ShspNetworkException(
      'Failed to list network interfaces',
      cause: e,
    );
  }
}