@@ -96,7 +96,7 @@ class MDnsVmServiceDiscovery {
9696final List <MDnsVmServiceDiscoveryResult > results= await _pollingVmService (
9797 _preliminaryClient?? MDnsClient (),
9898 applicationId: applicationId,
99- deviceVmservicePort : deviceVmservicePort,
99+ deviceVmServicePort : deviceVmservicePort,
100100 ipv6: ipv6,
101101 useDeviceIPAsHost: useDeviceIPAsHost,
102102 timeout: const Duration (seconds: 5 ),
@@ -192,7 +192,7 @@ class MDnsVmServiceDiscovery {
192192final List <MDnsVmServiceDiscoveryResult > results= await _pollingVmService (
193193 client,
194194 applicationId: applicationId,
195- deviceVmservicePort : deviceVmservicePort,
195+ deviceVmServicePort : deviceVmservicePort,
196196 deviceName: deviceName,
197197 ipv6: ipv6,
198198 useDeviceIPAsHost: useDeviceIPAsHost,
@@ -208,7 +208,67 @@ class MDnsVmServiceDiscovery {
208208Future <List <MDnsVmServiceDiscoveryResult >>_pollingVmService (
209209MDnsClient client, {
210210String ? applicationId,
211- int ? deviceVmservicePort,
211+ int ? deviceVmServicePort,
212+ String ? deviceName,
213+ bool ipv6= false ,
214+ bool useDeviceIPAsHost= false ,
215+ required Duration timeout,
216+ bool quitOnFind= false ,
217+ })async {
218+ final Completer <List <MDnsVmServiceDiscoveryResult >> completer=
219+ Completer <List <MDnsVmServiceDiscoveryResult >>();
220+ unawaited (
221+ runZonedGuarded (
222+ ()async {
223+ completer.complete (
224+ await _doPollingVmService (
225+ client,
226+ applicationId: applicationId,
227+ deviceVmServicePort: deviceVmServicePort,
228+ deviceName: deviceName,
229+ ipv6: ipv6,
230+ useDeviceIPAsHost: useDeviceIPAsHost,
231+ timeout: timeout,
232+ quitOnFind: quitOnFind,
233+ ),
234+ );
235+ },
236+ (Object error,StackTrace stackTrace) {
237+ if (! completer.isCompleted) {
238+ completer.completeError (error, stackTrace);
239+ }
240+ },
241+ ),
242+ );
243+
244+ try {
245+ return await completer.future;
246+ }on SocketException catch (e, stackTrace) {
247+ if (! globals.platform.isMacOS) {
248+ rethrow ;
249+ }
250+
251+ _logger.printTrace (stackTrace.toString ());
252+
253+ throwToolExit (
254+ 'Flutter could not connect to the Dart VM service.\n '
255+ '\n '
256+ 'Please ensure your IDE or terminal app has permission to access '
257+ 'devices on the local network. This allows Flutter to connect to '
258+ 'the Dart VM.\n '
259+ '\n '
260+ 'You can grant this permission in System Settings > Privacy & '
261+ 'Security > Local Network.\n '
262+ '\n '
263+ '$e '
264+ );
265+ }
266+ }
267+
268+ Future <List <MDnsVmServiceDiscoveryResult >>_doPollingVmService (
269+ MDnsClient client, {
270+ String ? applicationId,
271+ int ? deviceVmServicePort,
212272String ? deviceName,
213273bool ipv6= false ,
214274bool useDeviceIPAsHost= false ,
@@ -232,27 +292,10 @@ class MDnsVmServiceDiscovery {
232292final Set <String > uniqueDomainNamesInResults= < String > {};
233293
234294// Listen for mDNS connections until timeout.
235- final Stream <PtrResourceRecord > ptrResourceStream;
236-
237- try {
238- ptrResourceStream= client.lookup <PtrResourceRecord >(
239- ResourceRecordQuery .serverPointer (dartVmServiceName),
240- timeout: timeout,
241- );
242- }on SocketException catch (e, stacktrace) {
243- _logger.printError (e.message);
244- _logger.printTrace (stacktrace.toString ());
245- if (globals.platform.isMacOS) {
246- throwToolExit (
247- 'You might be having a permissions issue with your IDE. '
248- 'Please try going to '
249- 'System Settings -> Privacy & Security -> Local Network -> '
250- '[Find your IDE] -> Toggle ON, then restart your phone.' ,
251- );
252- }else {
253- rethrow ;
254- }
255- }
295+ final Stream <PtrResourceRecord > ptrResourceStream= client.lookup <PtrResourceRecord >(
296+ ResourceRecordQuery .serverPointer (dartVmServiceName),
297+ timeout: timeout,
298+ );
256299
257300await for (final PtrResourceRecord ptrin ptrResourceStream) {
258301 uniqueDomainNames.add (ptr.domainName);
@@ -292,8 +335,8 @@ class MDnsVmServiceDiscovery {
292335 );
293336 }
294337
295- // IfdeviceVmservicePort is set, only use records that match it
296- if (deviceVmservicePort != null && srvRecord.port!= deviceVmservicePort ) {
338+ // IfdeviceVmServicePort is set, only use records that match it
339+ if (deviceVmServicePort != null && srvRecord.port!= deviceVmServicePort ) {
297340continue ;
298341 }
299342
@@ -355,8 +398,8 @@ class MDnsVmServiceDiscovery {
355398// the applicationId were found but other results were found, throw an error.
356399if (applicationId!= null && quitOnFind&& results.isEmpty&& uniqueDomainNames.isNotEmpty) {
357400String message= 'Did not find a Dart VM Service advertised for $applicationId ' ;
358- if (deviceVmservicePort != null ) {
359- message+= ' on port $deviceVmservicePort ' ;
401+ if (deviceVmServicePort != null ) {
402+ message+= ' on port $deviceVmServicePort ' ;
360403 }
361404throwToolExit ('$message .' );
362405 }