prompt method
Asks question and answers the line typed, defaultValue for an
empty one (the empty string without one).
validate refuses an answer with a message, which is shown, and the
question is asked again. With hidden the answer is not shown as it
is typed: masked with one * per character in the interactive mode,
not echoed in the plain one; written back, it reads ******
whatever its length.
How it asks follows the mode:
- CliMode.interactive: a
TextFieldin the inline session, after the question and the default,placeholderin it while it is empty; a refusal under it until the text changes.suggestgives what to choose from for the text before the caret, shown under it (Autocomplete: Up and Down choose, Enter or Tab put it in). Once answered the field goes and✔ question answeris committed in its place. Ctrl+C throws PromptCancelledException with PromptCancelReason.interrupt, Ctrl+D at an empty field with PromptCancelReason.endOfInput,✗ questioncommitted. - CliMode.plain:
? question (default), then the line read as the terminal reads it; the end of input throws PromptCancelledException with PromptCancelReason.endOfInput, and Ctrl+C is the terminal's: SIGINT. - CliMode.nonInteractive: nothing is read,
defaultValueis the answer, written to the output as? question answerso a log shows what was assumed. Without one, or with onevalidaterefuses, PromptAnswerRequiredException.
Prompts are asked one at a time: one asked while another is waits for it.
final name = await Cli.current.prompt(
'Project name?',
defaultValue: 'my_app',
validate: (name) => RegExp(r'^[a-z_]+$').hasMatch(name)
? null
: 'lowercase letters and _ only',
);
Implementation
Future<String> prompt(
String question, {
String? defaultValue,
bool hidden = false,
String? Function(String answer)? validate,
FutureOr<List<String>> Function(String typed)? suggest,
String? placeholder,
}) => _prompter.prompt(
question,
defaultValue: defaultValue,
hidden: hidden,
validate: validate,
suggest: suggest,
placeholder: placeholder,
);