Search…
User input
For complete API documentation refer to: pub.dev
Asking the user to input data into a CLI application should be simple. DCli provide a number of core methods to facilitate user input.
  • ask
  • confirm
  • menu

Ask

The 'ask' function provides a simple but flexible means of requesting information from the user.
In its simplest form you can ask the user for an input string.
Any user input has whitespace stripped before it is validated or returned.

Arguments

prompt

The prompt is the only positional argument that ask takes. Ask will display the prompt verbatim.
1
var username = ask('Username:');
Copied!
1
Username: brett
Copied!
You may pass an empty string for the prompt in which case no prompt will be displayed.

hidden

You can request that the user input isn't echoed back to the user:
1
var password = ask('Password:', hidden: true);
Copied!
1
Password: ******
Copied!

defaultValue

You can provide a default value. If the user hits enter without entering any text then the default value will be returned.
1
var username = ask('Username:', defaultValue: 'Administrator');
Copied!
1
Username: Administrator
Copied!
If you combine a defaultValue with the hidden argument then the default value will be rendered as 6 '*'.
1
var password = ask('Password:', hidden: true, defaultValue: 'a secret');
Copied!
1
password: ******
Copied!
If you combine a defaultValue with an empty prompt then Ask will not display the prompt nor the default value.
1
var secretQuestion = ask('', defaultValue: 'a secret', required: false);
Copied!
1
Copied!

validator

Ask takes a validator. If the entered input doesn't match the supplied validator then the user will be re-prompted until they enter a valid value.
1
var age = ask('Age:', validator: Ask.integer);
Copied!
1
Age: abc
2
Invalid integer.
3
Age:
Copied!
See the section on validators for more details.

required

By default the ask function requires the user to enter a non-blank line (whitespace is stripped from user input before it is evaluated).
If you want to make a user value optional either pass in a defaultValue or pass required: false
1
var age = ask('Age:', required: false, validator: Ask.integer);
Copied!

Confirm

The confirm method allows you to ask the user for a true/false response and returns a bool reflecting what the user entered.
1
bool allowed = confirm('Are you over 18', defaultValue: false);
Copied!

Menu

The menu function allows you to display a list of menu items for the user to select from and returns the selected item.
1
var selected = menu('Select your poison'
2
, options: ['beer', 'wine', 'spirits']
3
, defaultOption: 'beer');
4
print(green('You chose $selected'));
Copied!
1
1) beer
2
2) wine
3
3) spirits
4
Selection your poison: 1
Copied!
You can also specify a default option. If you pass a default value and the user hits enter without entering a value then the default value will be returned.
The list of options can be a String or a Dart class. By default menu will call toString on any object passed but you can pass the format argument to control how each option is displayed:
1
class Car
2
{
3
String make;
4
String model;
5
Car(this.make, this.model);
6
}
7
var selected = menu('Choose your preferred car:'
8
, options: [Car('Ford', 'Falcon'), Car('Holden', 'Capree'), Car('BMW', 'M3')]
9
, format: (car) => '${car.make} ${car.model}'
10
, defaultOption: 'beer');
11
print(green('You chose $selected'));
Copied!
1
1) Ford Falcon
2
2) Holden Capree
3
3) BMW M3
4
Choose your preferred car: 1
Copied!
Last modified 1mo ago
Copy link