Skip to content

Commit 60eb82f

Browse files
[multicast_dns] MDnsClient::listen supports onError callback (#8888)
MDnsClient::Listen now supports an optional onError callback function, called in case of a stream error. If omitted any errors on the stream are considered unhandled, and will be passed to the current [Zone]'s error handler. By default unhandled async errors are treated as if they were uncaught top-level errors. This fixes an unhandled exception occuring (tested on Android), when the network is disconnected. Issue: flutter/flutter#165482
1 parent a1c3689 commit 60eb82f

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

packages/multicast_dns/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## NEXT
2+
3+
* Optional error callback for `MDnsClient::start` to prevent uncatched exceptions.
4+
15
## 0.3.2+8
26

37
* Fixes stack overflows ocurring during the parsing of domain names in MDNS messages.

packages/multicast_dns/lib/multicast_dns.dart

+11-1
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,20 @@ class MDnsClient {
8484
/// for the mDNS query. If not provided, defaults to either `224.0.0.251` or
8585
/// or `FF02::FB`.
8686
///
87+
/// The [onError] function allows to provide a callback function, called in
88+
/// case of a stream error. If omitted any errors on the stream are considered
89+
/// unhandled, and will be passed to the current [Zone]'s error handler. By
90+
/// default unhandled async errors are treated as if they were uncaught top-level
91+
/// errors.
92+
///
8793
/// Subsequent calls to this method are ignored while the mDNS client is in
8894
/// started state.
8995
Future<void> start({
9096
InternetAddress? listenAddress,
9197
NetworkInterfacesFactory? interfacesFactory,
9298
int mDnsPort = mDnsPort,
9399
InternetAddress? mDnsAddress,
100+
Function? onError,
94101
}) async {
95102
listenAddress ??= InternetAddress.anyIPv4;
96103
interfacesFactory ??= allInterfacesFactory;
@@ -152,7 +159,10 @@ class MDnsClient {
152159
// Join multicast on this interface.
153160
incoming.joinMulticast(_mDnsAddress!, interface);
154161
}
155-
incoming.listen((RawSocketEvent event) => _handleIncoming(event, incoming));
162+
incoming.listen(
163+
(RawSocketEvent event) => _handleIncoming(event, incoming),
164+
onError: onError,
165+
);
156166
_started = true;
157167
_starting = false;
158168
}

0 commit comments

Comments
 (0)