Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to read from console in Dart?

Tags:

console

dart

I know that we can print to the console in dart using the print() statement.

I want to know if it is possible to read data from console. I did a search and also looked in the dart:io package, but couldn't find any reference.

Thanks

like image 969
Sudar Avatar asked Apr 21 '12 09:04

Sudar


People also ask

What is stdout in dart?

Stdout represents the IOSink for either stdout or stderr . It provides a blocking IOSink , so using this to write will block until the output is written. In some situations this blocking behavior is undesirable as it does not provide the same non-blocking behavior as dart:io in general exposes.

How do you print on the same line in darts?

Dart – Print without Newline To print a string to console without a trailing new line in Dart, import dart:io library, and call stdout. write() function with the string passed as argument to it.


1 Answers

You can use StringInputStream to read from stdin like this

#import("dart:io");

main() { 
  var stream = new StringInputStream(stdin); 
  stream.onLine = () { 
    var line = stream.readLine(); 
    if (line != null) { 
      print(line); 
    } 
  }; 
} 

also if you're developing a console application then checkout the Options class to parse command line arguments

final args = new Options().arguments;
like image 70
Lars Tackmann Avatar answered Sep 24 '22 00:09

Lars Tackmann