diff --git a/packages/flutter_tools/lib/src/commands/daemon.dart b/packages/flutter_tools/lib/src/commands/daemon.dart index 3be775f4f2..cc5bb60fe9 100644 --- a/packages/flutter_tools/lib/src/commands/daemon.dart +++ b/packages/flutter_tools/lib/src/commands/daemon.dart @@ -179,10 +179,10 @@ class Daemon { void _send(Map map) => sendCommand(map); - void shutdown({ dynamic error }) { - _commandSubscription?.cancel(); + Future shutdown({ dynamic error }) async { + await _commandSubscription?.cancel(); for (final Domain domain in _domainMap.values) { - domain.dispose(); + await domain.dispose(); } if (!_onExitCompleter.isCompleted) { if (error == null) { @@ -273,7 +273,7 @@ abstract class Domain { return val as int; } - void dispose() { } + Future dispose() async { } } /// This domain responds to methods like [version] and [shutdown]. @@ -351,8 +351,8 @@ class DaemonDomain extends Domain { } @override - void dispose() { - _subscription?.cancel(); + Future dispose() async { + await _subscription?.cancel(); } /// Enumerates the platforms supported by the provided project. @@ -828,9 +828,9 @@ class DeviceDomain extends Domain { } @override - void dispose() { + Future dispose() async { for (final PollingDeviceDiscovery discoverer in _discoverers) { - discoverer.dispose(); + await discoverer.dispose(); } } diff --git a/packages/flutter_tools/lib/src/device.dart b/packages/flutter_tools/lib/src/device.dart index 2990321119..4f8e9a9915 100644 --- a/packages/flutter_tools/lib/src/device.dart +++ b/packages/flutter_tools/lib/src/device.dart @@ -352,7 +352,7 @@ abstract class PollingDeviceDiscovery extends DeviceDiscovery { return deviceNotifier.onRemoved; } - void dispose() => stopPolling(); + Future dispose() async => await stopPolling(); @override String toString() => '$name device discovery'; diff --git a/packages/flutter_tools/lib/src/ios/devices.dart b/packages/flutter_tools/lib/src/ios/devices.dart index 674a6fcc52..7d832395cd 100644 --- a/packages/flutter_tools/lib/src/ios/devices.dart +++ b/packages/flutter_tools/lib/src/ios/devices.dart @@ -43,11 +43,6 @@ class IOSDevices extends PollingDeviceDiscovery { _logger = logger ?? globals.logger, super('iOS devices'); - @override - void dispose() { - _observedDeviceEventsSubscription?.cancel(); - } - final Platform _platform; final XCDevice _xcdevice; final IOSWorkflow _iosWorkflow; diff --git a/packages/flutter_tools/test/general.shard/ios/devices_test.dart b/packages/flutter_tools/test/general.shard/ios/devices_test.dart index 522c976b88..01cf31b35c 100644 --- a/packages/flutter_tools/test/general.shard/ios/devices_test.dart +++ b/packages/flutter_tools/test/general.shard/ios/devices_test.dart @@ -482,6 +482,28 @@ void main() { expect(rescheduledStream.hasListener, isFalse); }); + testWithoutContext('dispose cancels polling subscription', () async { + final IOSDevices iosDevices = IOSDevices( + platform: macPlatform, + xcdevice: mockXcdevice, + iosWorkflow: mockIosWorkflow, + logger: logger, + ); + when(mockXcdevice.isInstalled).thenReturn(true); + when(mockXcdevice.getAvailableIOSDevices()) + .thenAnswer((Invocation invocation) => Future>.value([])); + + final StreamController> eventStream = StreamController>(); + when(mockXcdevice.observedDeviceEvents()).thenAnswer((_) => eventStream.stream); + + await iosDevices.startPolling(); + expect(iosDevices.deviceNotifier.items, isEmpty); + expect(eventStream.hasListener, isTrue); + + await iosDevices.dispose(); + expect(eventStream.hasListener, isFalse); + }); + final List unsupportedPlatforms = [linuxPlatform, windowsPlatform]; for (final Platform unsupportedPlatform in unsupportedPlatforms) { testWithoutContext('pollingGetDevices throws Unsupported Operation exception on ${unsupportedPlatform.operatingSystem}', () async {