ask static method

String ask(
  1. String question, {
  2. String? defaultValue,
})

Prompts the user with a question and reads a line from stdin.

Implementation

static String ask(String question, {String? defaultValue}) {
  final suffix = defaultValue != null ? ' ($defaultValue)' : '';
  stdout.write('$question$suffix: ');
  final input = stdin.readLineSync()?.trim();
  if (input == null || input.isEmpty) {
    if (defaultValue != null) return defaultValue;
    return ask(question, defaultValue: defaultValue);
  }
  return input;
}