Introduction
Local Network Discovery using Multicast DNS (mDNS) allows Flutter apps to find and advertise services on the same LAN without a central server. This technique powers peer-to-peer features such as media streaming, multiplayer gaming, and IoT device control. In this tutorial, we’ll explore how to integrate mDNS into your Flutter mobile application, covering setup, service discovery, and service registration.
Understanding mDNS in Flutter
mDNS is a zero-configuration networking protocol that uses multicast UDP packets to resolve hostnames and service types in a local network. In Flutter, you can leverage the multicast_dns package to perform DNS-SD (DNS Service Discovery) lookups. The client listens for PTR, SRV, TXT, and A or AAAA records corresponding to advertised services. When a device advertises a service (for example, _http._tcp.local.), PTR records direct you to SRV records containing the target host and port, while TXT records provide metadata.
Adding Dependencies and Permissions
To get started, add multicast_dns to your pubspec.yaml:
dependencies:
flutter:
sdk: flutter
multicast_dns
On Android, declare the following permission in AndroidManifest.xml to allow multicast traffic:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"
On iOS, no extra entitlements are required beyond the default network permissions.
Discovering Services on the Local Network
To discover services, initialize an MDnsClient, then query for PTR records under the service type. Here’s a concise example that finds all HTTP servers advertising on port 80:
import 'package:multicast_dns/multicast_dns.dart';
Future<void> discoverHttpServices() async {
final MDnsClient client = MDnsClient();
await client.start();
await for (final PtrResourceRecord ptr in client.lookup<PtrResourceRecord>(
ResourceRecordQuery.serverPointers('_http._tcp.local'),
)) {
await for (final SrvResourceRecord srv in client.lookup<SrvResourceRecord>(
ResourceRecordQuery.service(ptr.domainName),
)) {
print('Discovered service at ${srv.target}:${srv.port}');
}
}
client.stop();
}This snippet listens for PTR records under _http._tcp.local, then fetches the corresponding SRV records to extract each service’s host and port.
Registering Your Own Service
If your app needs to advertise a service, you can register it with mDNS. While multicast_dns focuses on discovery, you can combine it with dns_server or a custom UDP advertiser. Below is a conceptual example of broadcasting an HTTP service on port 8080 (requires low-level socket code or an external package for full implementation):
import 'package:multicast_dns/multicast_dns.dart';
Future<void> registerService() async {
final MDnsClient advertiser = MDnsClient();
await advertiser.start();
advertiser.register(
name: 'Flutter Http Service',
type: MDnsResourceRecordType.A,
port: 8080,
address: InternetAddress('192.168.1.100'),
);
}Real-world registration often requires implementing a small responder that listens for mDNS queries and sends appropriate records. Consider using dart:io sockets to craft responses if you need full control.
Handling Discovered Services
After discovering a service, you typically want to connect or fetch metadata via HTTP or sockets. Use the resolved IP and port in your network client:
final uri = Uri.http('${srv.target}', '/status');
final response = await http.get(uri);
print('Service status: ${response.body}');Always handle exceptions for unreachable devices or network changes. Stop the MDnsClient when the app disposes to free up sockets.
Vibe Studio

Vibe Studio, powered by Steve’s advanced AI agents, is a revolutionary no-code, conversational platform that empowers users to quickly and efficiently create full-stack Flutter applications integrated seamlessly with Firebase backend services. Ideal for solo founders, startups, and agile engineering teams, Vibe Studio allows users to visually manage and deploy Flutter apps, greatly accelerating the development process. The intuitive conversational interface simplifies complex development tasks, making app creation accessible even for non-coders.
Conclusion
Implementing local network discovery in Flutter with mDNS enables powerful peer-to-peer capabilities without a central server. By adding the multicast_dns package, configuring platform permissions, and writing concise lookup and (optionally) registration code, you can discover and advertise services seamlessly. Integrate these patterns to enhance your Flutter mobile applications with zero-configuration networking.