diff --git a/packages/flutter_tools/lib/src/build_runner/web_fs.dart b/packages/flutter_tools/lib/src/build_runner/web_fs.dart index 10bf12bb7b..5fd49e40a9 100644 --- a/packages/flutter_tools/lib/src/build_runner/web_fs.dart +++ b/packages/flutter_tools/lib/src/build_runner/web_fs.dart @@ -90,6 +90,7 @@ class WebFs { final Dwds _dwds; final Chrome _chrome; final BuildDaemonClient _client; + StreamSubscription _connectedApps; static const String _kHostName = 'localhost'; @@ -98,13 +99,20 @@ class WebFs { await _dwds.stop(); await _server.close(force: true); await _chrome.close(); + await _connectedApps?.cancel(); } /// Retrieve the [DebugConnection] for the current application. Future runAndDebug() async { - final AppConnection appConnection = await _dwds.connectedApps.first; - appConnection.runMain(); - return _dwds.debugConnection(appConnection); + final Completer firstConnection = Completer(); + _connectedApps = _dwds.connectedApps.listen((AppConnection appConnection) async { + appConnection.runMain(); + final DebugConnection debugConnection = await _dwds.debugConnection(appConnection); + if (!firstConnection.isCompleted) { + firstConnection.complete(debugConnection); + } + }); + return firstConnection.future; } /// Perform a hard refresh of all connected browser tabs.