chooseOne<T extends Object?> method

Future<T> chooseOne<T extends Object?>(
  1. String? message, {
  2. required List<T> choices,
  3. T? defaultValue,
  4. String display(
    1. T choice
    )?,
})

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];
}