Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 689
lg.lindstrom Avatar asked Oct 17 '25 12:10

lg.lindstrom


1 Answers

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.

like image 174
Josip Domazet Avatar answered Oct 20 '25 03:10

Josip Domazet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!