Nimrodda / flutter_nsd

A Flutter plugin that enables Network Service Discovery (mDNS) on various platforms via their native APIs.
Apache License 2.0
34 stars 8 forks source link

stopDiscovery() stops, but generates an error #44

Closed mdev88 closed 1 year ago

mdev88 commented 1 year ago

If I start the discovery and then stop it with stopDiscovery(), I receive a "NsdErrorCode.discoveryStopped" error in the stream listener. Is this by design or am I doing something wrong here?

Thanks!

This is my implementation:

    flutterNsd.stream.listen((nsdServiceInfo) {
      log('Discovered service name: ${nsdServiceInfo.name}');
      log('Discovered service hostname/IP: ${nsdServiceInfo.hostname}');
      log('Discovered service port: ${nsdServiceInfo.port}');
    }, onError: (e) {
      if (e is NsdError) {
        log('NSD ERROR: ${e.errorCode}');  // Why does stopping NSD throws an error? Is this by design?
      } else {
        log('NSD ERROR: $e');
      }
    }, onDone: (){
      log('NSD Done');
    });
  }
Nimrodda commented 1 year ago

You're right, it shouldn't be an error. It was kind of simple solution to avoid extra logic in the listener. This is the commit that introduced it https://github.com/Nimrodda/flutter_nsd/commit/93fe2a4fb49a66cbf24d86b85c090d9d012b69f3 I'm open to suggestions/contributions if you'd like to change it.

mdev88 commented 1 year ago

I don't think I'm capable of proposing a better way, so I'll keep using the onError callback knowing that it is by design.

Thanks!