lumi_flutter_nsd 0.0.1 copy "lumi_flutter_nsd: ^0.0.1" to clipboard
lumi_flutter_nsd: ^0.0.1 copied to clipboard

A Flutter plugin for discovering network services using NSD (Network Service Discovery) on Android and Bonjour on iOS. This plugin enables your app to find services on a local network.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:lumi_flutter_nsd/nsd.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  List<NsdService> services = [];
  bool isDiscovering = false;

  @override
  void initState() {
    super.initState();
    Nsd.setDebug(true);
    addListener();
  }

  @override
  void dispose() {
    super.dispose();
    stopDiscovery();
    Nsd.dispose();
  }

  void addListener() {
    Nsd.serviceFound.listen((data) {
      setState(() {
        services = [...services, data];
      });
    });

    Nsd.discoverServiceStarted.listen((_) {
      setState(() {
        isDiscovering = true;
      });
    });

    Nsd.discoverServiceStopped.listen((_) {
      setState(() {
        isDiscovering = false;
      });
    });

    Nsd.discoverServiceFailed.listen((_) {
      setState(() {
        isDiscovering = false;
      });
    });

    Nsd.serviceLost.listen((service) {
      setState(() {
        services = services.where((s) => s.name != service.name).toList();
      });
    });
  }

  void startDiscovery() async {
    setState(() {
      isDiscovering = true;
      services = [];
    });
    await Nsd.discoverServices();
  }

  void stopDiscovery() async {
    await Nsd.stopDiscovery();
    setState(() {
      isDiscovering = false;
    });
  }

  @override
  Widget build(BuildContext context) {
    print("MyApp::build:---------> $services");
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Discovery MDNS'),
          actions: [
            IconButton(
              onPressed: isDiscovering ? stopDiscovery : startDiscovery,
              icon: isDiscovering
                  ? const Icon(Icons.stop)
                  : const Icon(Icons.search),
            ),
          ],
        ),
        body: ListView.builder(
          itemCount: services.length,
          itemBuilder: (context, index) {
            final service = services[index];
            return ListTile(
              title: Text(service.name),
              subtitle: Text('${service.ip}:${service.port}'),
              // subtitle: Text(service.attributes?.toString() ?? ''),
              // backgroundColor: Colors.blue,
            );
          },
        ),
      ),
    );
  }
}
1
likes
140
points
16
downloads

Publisher

unverified uploader

Weekly Downloads

A Flutter plugin for discovering network services using NSD (Network Service Discovery) on Android and Bonjour on iOS. This plugin enables your app to find services on a local network.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on lumi_flutter_nsd

Packages that implement lumi_flutter_nsd