prompt function

String prompt(
  1. String message, {
  2. String? defaultValue,
})

Prompts the user with message and returns their input. Optionally provides a defaultValue if they press enter without typing.

Implementation

String prompt(String message, {String? defaultValue}) {
  final defaultStr = defaultValue != null ? ' [$defaultValue]' : '';
  stdout.write('$message$defaultStr: ');
  final input = stdin.readLineSync()?.trim();
  if (input == null || input.isEmpty) {
    return defaultValue ?? '';
  }
  return input;
}