prompt method
Asks the user a question and returns their response
Implementation
String prompt(String question, {String defaultValue = ''}) {
// Print the question and default value if provided
if (defaultValue.isNotEmpty) {
print('$question (default: $defaultValue)');
} else {
print('$question');
}
// Explicitly flush stdout to ensure the prompt is displayed
stdout.flush();
// Try to read input in a more robust way
String? input;
try {
input = stdin.readLineSync();
} catch (e) {
error('Error reading input: $e');
// Fall back to default value in case of error
return defaultValue;
}
// Trim the input and handle null/empty cases
final trimmedInput = input?.trim() ?? '';
// Return default value if user just presses Enter
return trimmedInput.isEmpty ? defaultValue : trimmedInput;
}