Reading from stdin
In Dart, user input from the command line is read using stdin.readLineSync() from the dart:io library. This reads one line of text entered by the user.
import 'dart:io';
void main() {
stdout.write('Enter your name: '); // write without newline
String? name = stdin.readLineSync();
print('Hello, $name!');
}
Reading a String
import 'dart:io';
void main() {
print('What is your favourite language?');
String? lang = stdin.readLineSync();
print('Great choice: $lang!');
}
Note: stdin.readLineSync() returns String? (nullable String), because it can return null if the input stream is closed.
Reading a Number
User input is always a String. You must parse it to use it as a number:
import 'dart:io';
void main() {
stdout.write('Enter your age: ');
String? input = stdin.readLineSync();
int age = int.parse(input!); // parse to int
if (age >= 18) {
print('You are an adult.');
} else {
print('You are a minor.');
}
}
Null Safety with Input
Since readLineSync() returns String?, handle null values safely:
import 'dart:io';
void main() {
stdout.write('Enter a number: ');
String? input = stdin.readLineSync();
// Safe approach with null check
if (input == null || input.isEmpty) {
print('No input provided.');
return;
}
int? number = int.tryParse(input);
if (number == null) {
print('Invalid number!');
} else {
print('Double: $${number * 2}');
}
}
Full Example
import 'dart:io';
void main() {
// Get student info
stdout.write('Enter student name: ');
String name = stdin.readLineSync() ?? 'Unknown';
stdout.write('Enter score (0-100): ');
String? scoreInput = stdin.readLineSync();
double score = double.tryParse(scoreInput ?? '') ?? 0.0;
// Calculate grade
String grade;
if (score >= 90) grade = 'A';
else if (score >= 80) grade = 'B';
else if (score >= 70) grade = 'C';
else if (score >= 60) grade = 'D';
else grade = 'F';
print('\n--- Result ---');
print('Student: $name');
print('Score: $score');
print('Grade: $grade');
}
Sample Run:
Enter student name: Alice
Enter score (0-100): 88
--- Result ---
Student: Alice
Score: 88.0
Grade: B
⚠️ Common Mistakes
- Forgetting
import 'dart:io'; — stdin is not available without it.
- Not parsing the input before arithmetic:
int age = stdin.readLineSync(); is a type error.
- Using
input! (null assertion) without checking if input is null — causes a runtime error.
💪 Exercise
- Write a program that asks for two numbers and prints their sum, difference, product, and quotient.
- Create a simple "Name Age City" profile input program that prints a formatted summary.
- Add validation: if the user enters a non-number when a number is expected, print an error message.
🧠 Quiz
1. What library provides stdin in Dart?
- A) dart:core
- B) dart:io ✅
- C) dart:input
- D) dart:console
2. What type does stdin.readLineSync() return?
- A) String
- B) String? ✅
- C) int
- D) dynamic
Summary
Read user input in Dart using stdin.readLineSync() from dart:io. It returns a nullable String?, so always handle potential null values. Parse to numbers with int.parse() or safe int.tryParse(). Use stdout.write() for prompts without newlines.