ask static method

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

Prompts the user with a question and returns the entered value.

Returns defaultValue when the user provides no input and a default value is available.

Implementation

static String ask(
  String question, {
  String? defaultValue,
}) {
  if (defaultValue != null && defaultValue.isNotEmpty) {
    stdout.write('$question [$defaultValue]: ');
  } else {
    stdout.write('$question: ');
  }

  final input = stdin.readLineSync()?.trim() ?? '';

  if (input.isEmpty && defaultValue != null) {
    return defaultValue;
  }

  return input;
}