selectIntegrationDevice function
Selects an Android/iOS emulator, simulator, or physical device for integration mode.
Implementation
Future<FlutterDevice> selectIntegrationDevice(
List<String> arguments, {
String flutterExecutable = 'flutter',
Future<List<FlutterDevice>> Function()? discover,
}) async {
final devices = await (discover ??
() => discoverFlutterDevices(executable: flutterExecutable))();
final supported =
devices.where((device) => device.isSupportedIntegrationTarget).toList();
final requested = _optionValues(arguments, '--device-id');
if (requested.isNotEmpty) {
final matches = devices.where((device) => device.id == requested.single);
if (matches.isEmpty) {
throw StateError(
'Flutter device "${requested.single}" was not found.\n'
'${formatDeviceList(devices)}',
);
}
final selected = matches.single;
if (!selected.isSupportedIntegrationTarget) {
throw StateError(
'Device "${selected.display}" is not supported. Select an Android or '
'iOS emulator, simulator, or physical device.\n'
'${formatDeviceList(devices)}',
);
}
return selected;
}
if (supported.isEmpty) {
throw StateError(
'No supported integration target is connected. Start an Android '
'emulator/iOS simulator or connect a physical Android/iOS device.\n'
'${formatDeviceList(devices)}',
);
}
if (supported.length == 1) return supported.single;
if (!stdin.hasTerminal) {
throw StateError(
'Multiple integration targets are connected. Select one with '
'--device-id=<id>:\n${formatDeviceList(supported)}',
);
}
stderr.writeln('Select an integration target:');
for (var i = 0; i < supported.length; i++) {
stderr.writeln(' ${i + 1}) ${supported[i].display}');
}
stderr.write('Device: ');
final selection = int.tryParse(stdin.readLineSync()?.trim() ?? '');
if (selection == null || selection < 1 || selection > supported.length) {
throw StateError('No valid integration target was selected.');
}
return supported[selection - 1];
}