Dart User Input
Dart User Input
A console program can ask the user to type something, like a name or a number.
To read what the user types, use stdin.readLineSync() from the dart:io library.
It waits until the user presses Enter, and then returns the line as a String.
Example
import 'dart:io';
void main() {
print('What is your name?');
String? name = stdin.readLineSync();
print('Hello, $name');
}
Try it Yourself »
Result:
What is your name?
Hello, null
The result above is from a run where nobody typed anything.
That is why it says null.
Why Can the Input Be null?
The return type of readLineSync() is String?, a nullable string.
It returns null when there is no more input to read.
This happens when the program runs without a person typing, like in the run that made the result above.
If you run the program on your own computer with dart run, and type Anna, the result is:
Result:
What is your name?
Anna
Hello, Anna
Handle null With ??
A good program always handles the null case.
The if-null operator ?? gives a default value when the input is null:
Example
import 'dart:io';
void main() {
print('What is your name?');
String name = stdin.readLineSync() ?? 'Guest';
print('Hello, $name');
}
Try it Yourself »
Result:
What is your name?
Hello, Guest
Now name is a normal String, and it is never null.
Use stdout.write() instead of print() if you want the user to type on the same line as the question:
Example
import 'dart:io';
void main() {
stdout.write('Enter your city: ');
String city = stdin.readLineSync() ?? 'Unknown';
print('You live in $city');
}
Try it Yourself »
Result:
Enter your city: You live in Unknown
Since nobody typed anything here, the next output comes right after the question, on the same line.
Clean Up the Input
Users often type extra spaces by mistake.
Use trim() to remove spaces at the start and the end.
The ?. operator calls trim() only if the input is not null:
Example
import 'dart:io';
void main() {
print('Pick a color:');
String color = stdin.readLineSync()?.trim() ?? '';
if (color.isEmpty) {
print('You did not pick a color');
} else {
print('You picked $color');
}
}
Try it Yourself »
Result:
Pick a color:
You did not pick a color
Read a Number
The input is always text, even when the user types a number.
To use it as a number, convert it with int.tryParse().
tryParse() returns null if the text is not a valid number, so the program does not crash when the user types abc:
Example
import 'dart:io';
void main() {
print('How old are you?');
String input = stdin.readLineSync() ?? '';
int? age = int.tryParse(input);
if (age == null) {
print('That is not a valid age');
} else {
print('Next year you will be ${age + 1}');
}
}
Try it Yourself »
Result:
How old are you?
That is not a valid age
If you run it on your own computer and type 30, the result is:
Result:
How old are you?
30
Next year you will be 31
This program reads lines until there is no more input, and adds up the numbers:
Example
import 'dart:io';
void main() {
int total = 0;
String? line = stdin.readLineSync();
while (line != null) {
total += int.tryParse(line) ?? 0;
line = stdin.readLineSync();
}
print('Total: $total');
}
Try it Yourself »
Result:
Total: 0
Here there was no input at all, so the loop did not run and the total is 0.
Note: dart:io only works in programs that run on a computer or a server.
It does not work in Dart programs that run in a web browser.