Dart – Read a String from Console
To read a String from console in Dart, read a line from console using stdin.readLineSync()
method, and store the returned value in a variable.
Syntax
The syntax to read a line from console is
</>
Copy
stdin.readLineSync()
To use stdin.readLine()
method, import 'dart:io'
package. The method returns a string.
Examples
In the following program, we read a String from console into variable str
, and then print it back to console.
main.dart
</>
Copy
import 'dart:io';
void main() {
print('Enter a message');
String str = stdin.readLineSync()!;
print('You entered "$str"');
}
Output
Enter a message
Hello World
You entered "Hello World"
Conclusion
In this Dart Tutorial, we learned how to read a String from console in Dart language, with examples.