chooseOne<T extends Object?> method
Asks for one of choices, shown with display (default toString).
The cursor starts on defaultValue, or the first choice.
Implementation
Future<T> chooseOne<T extends Object?>(
String? message, {
required List<T> choices,
T? defaultValue,
String Function(T choice)? display,
}) async {
if (choices.isEmpty) {
throw ArgumentError.value(choices, 'choices', 'must not be empty');
}
final labels = [for (final c in choices) display?.call(c) ?? '$c'];
final start =
defaultValue == null ? 0 : max(0, choices.indexOf(defaultValue));
int? selectedIndex;
await _ask(
() => promptSelect(labels,
title: '$message', initial: start, options: _options),
onSelection: (indices) => selectedIndex = indices.single,
);
return choices[selectedIndex ?? start];
}