I am trying to read a string input from the console in a Dart application and print it. However, after entering the input, the output does not appear in the console. Here is my code:
import 'dart:io';
void main() {
String name = stdin.readLineSync()!; // input as `neel`
print(name);
}
When I run this code and input ‘neel’, nothing is printed to the console. How can I resolve this issue?
I am using Dart’s stdin.readLineSync() to read input from the console. My expectation is that after entering ‘neel’ and pressing Enter, the program should print ‘neel’ to the console. However, this does not happen, and I receive no output.
- I have checked the Dart documentation for stdin.readLineSync() and verified that my usage seems correct.
- I tried adding a flush to the stdout to ensure that the output buffer is cleared, but this did not resolve the issue.
- I ran the code in different environments (VS Code terminal, IntelliJ, and standard command line) but faced the same problem in each case.
Could someone help me understand why the input is not being printed and how to fix this issue? Any insights or suggestions would be greatly appreciated.
Manav Ramani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.