diff --git a/packages/flutter_tools/lib/src/ios/simulators.dart b/packages/flutter_tools/lib/src/ios/simulators.dart index 2d5db2b4fe..043f651318 100644 --- a/packages/flutter_tools/lib/src/ios/simulators.dart +++ b/packages/flutter_tools/lib/src/ios/simulators.dart @@ -90,7 +90,12 @@ class SimControl { return >{}; } try { - return json.decode(results.stdout)[section.name]; + final Object decodeResult = json.decode(results.stdout?.toString())[section.name]; + if (decodeResult is Map) { + return decodeResult; + } + printError('simctl returned unexpected JSON response: ${results.stdout}'); + return {}; } on FormatException { // We failed to parse the simctl output, or it returned junk. // One known message is "Install Started" isn't valid JSON but is @@ -107,9 +112,11 @@ class SimControl { final Map devicesSection = await _list(SimControlListSection.devices); for (String deviceCategory in devicesSection.keys) { - final List devicesData = devicesSection[deviceCategory]; - for (Map data in devicesData.map>(castStringKeyedMap)) { - devices.add(SimDevice(deviceCategory, data)); + final Object devicesData = devicesSection[deviceCategory]; + if (devicesData != null && devicesData is List) { + for (Map data in devicesData.map>(castStringKeyedMap)) { + devices.add(SimDevice(deviceCategory, data)); + } } } @@ -220,10 +227,10 @@ class SimDevice { final String category; final Map data; - String get state => data['state']; - String get availability => data['availability']; - String get name => data['name']; - String get udid => data['udid']; + String get state => data['state']?.toString(); + String get availability => data['availability']?.toString(); + String get name => data['name']?.toString(); + String get udid => data['udid']?.toString(); bool get isBooted => state == 'Booted'; } @@ -267,7 +274,7 @@ class IOSSimulator extends Device { Future isLatestBuildInstalled(ApplicationPackage app) async => false; @override - Future installApp(ApplicationPackage app) async { + Future installApp(covariant IOSApp app) async { try { final IOSApp iosApp = app; await SimControl.instance.install(id, iosApp.simulatorBundlePath); @@ -316,7 +323,7 @@ class IOSSimulator extends Device { @override Future startApp( - ApplicationPackage package, { + covariant IOSApp package, { String mainPath, String route, DebuggingOptions debuggingOptions, @@ -325,7 +332,7 @@ class IOSSimulator extends Device { bool usesTerminalUi = true, bool ipv6 = false, }) async { - if (!prebuiltApplication) { + if (!prebuiltApplication && package is BuildableIOSApp) { printTrace('Building ${package.name} for $id.'); try { @@ -371,8 +378,7 @@ class IOSSimulator extends Device { // which should always yield the correct value and does not require // parsing the xcodeproj or configuration files. // See https://github.com/flutter/flutter/issues/31037 for more information. - final IOSApp iosApp = package; - final String plistPath = fs.path.join(iosApp.simulatorBundlePath, 'Info.plist'); + final String plistPath = fs.path.join(package.simulatorBundlePath, 'Info.plist'); final String bundleIdentifier = iosWorkflow.getPlistValueFromFile(plistPath, kCFBundleIdentifierKey); await SimControl.instance.launch(id, bundleIdentifier, args); @@ -400,7 +406,7 @@ class IOSSimulator extends Device { } } - Future _setupUpdatedApplicationBundle(ApplicationPackage app, BuildInfo buildInfo, String mainPath, bool usesTerminalUi) async { + Future _setupUpdatedApplicationBundle(covariant BuildableIOSApp app, BuildInfo buildInfo, String mainPath, bool usesTerminalUi) async { await _sideloadUpdatedAssetsForInstalledApplicationBundle(app, buildInfo, mainPath); // Step 1: Build the Xcode project. @@ -422,8 +428,7 @@ class IOSSimulator extends Device { throwToolExit('Could not build the application for the simulator.'); // Step 2: Assert that the Xcode project was successfully built. - final IOSApp iosApp = app; - final Directory bundle = fs.directory(iosApp.simulatorBundlePath); + final Directory bundle = fs.directory(app.simulatorBundlePath); final bool bundleExists = bundle.existsSync(); if (!bundleExists) throwToolExit('Could not find the built application bundle at ${bundle.path}.'); @@ -463,11 +468,11 @@ class IOSSimulator extends Device { Future get sdkMajorVersion async { final Match sdkMatch = _iosSdkRegExp.firstMatch(await sdkNameAndVersion); - return int.parse(sdkMatch?.group(2) ?? 11); + return int.parse(sdkMatch?.group(2) ?? '11'); } @override - DeviceLogReader getLogReader({ ApplicationPackage app }) { + DeviceLogReader getLogReader({ covariant IOSApp app }) { assert(app is IOSApp); _logReaders ??= {}; return _logReaders.putIfAbsent(app, () => _IOSSimulatorLogReader(this, app)); diff --git a/packages/flutter_tools/test/general.shard/ios/simulators_test.dart b/packages/flutter_tools/test/general.shard/ios/simulators_test.dart index bafc54cd41..ce29a9a6a0 100644 --- a/packages/flutter_tools/test/general.shard/ios/simulators_test.dart +++ b/packages/flutter_tools/test/general.shard/ios/simulators_test.dart @@ -438,6 +438,12 @@ void main() { ProcessManager: () => mockProcessManager, SimControl: () => simControl, }); + + testUsingContext('sdkMajorVersion defaults to 11 when sdkNameAndVersion is junk', () async { + final IOSSimulator iosSimulatorA = IOSSimulator('x', name: 'Testo', simulatorCategory: 'NaN'); + + expect(await iosSimulatorA.sdkMajorVersion, 11); + }); }); group('startApp', () {