'How to find IP address using multicast_dns in flutter/dart?

I am writing a flutter application. To make life easier for end users I decided to find services running on a Raspberry Pi.

Avahi is running and working. I can use different mdns application to find the service and hostname AND IP for the host.

Flutter have a multicast_dns package but I cant find a way to get the IP of the host. Can anyone help??



Solution 1:[1]

After a lot of googling I have found a way to get the IP from the hostname. There is a method to resolve hostnames (ResourceRecordQuery.addressIPv4(srv.target)):

  await for (final PtrResourceRecord ptr in client.lookup<PtrResourceRecord>(
      ResourceRecordQuery.serverPointer(serviceType))) {
    print('PTR: ${ptr.toString()}');

    await for (final SrvResourceRecord srv in client.lookup<SrvResourceRecord>(
        ResourceRecordQuery.service(ptr.domainName))) {
      print('SRV target: ${srv.target} port: ${srv.port}');

      // Resolve the IP with this line
      await for (final IPAddressResourceRecord ip
          in client.lookup<IPAddressResourceRecord>(
              ResourceRecordQuery.addressIPv4(srv.target))) {
        print('IP: ${ip.address.toString()}');
      }
    }

I found this solution on a github thread.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Josip Domazet