lg.lindstrom
lg.lindstrom

Reputation: 856

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??

Upvotes: 4

Views: 1645

Answers (1)

Josip Domazet
Josip Domazet

Reputation: 2890

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.

Upvotes: 4

Related Questions