Could someone please show me an example of terminal input (question and response) in Dart (console) (latest r22223). The only example that I have seen doesn't appear to work or is incomplete.
Asked
Active
Viewed 539 times
2 Answers
4
Here's another option:
import "dart:async";
import "dart:io";
void main() {
stdout.write('> '); // stdout.write() rather than print() to avoid newline
new StringDecoder().bind(stdin).listen((str) { // Listen to a Stream<String>
print('"${str.trim()}"'); // Quote and parrot back the input
stdout.write('> '); // Prompt and keep listening
}, onDone: () => print('\nBye!')); // Stream is done, say bye
}
This appears to work fine on Linux and Windows. It quotes back to you whatever you enter at the prompt. You can exit by inputting EOF
(control-D
on Linux and other UNIX-like systems, control-Z
followed by enter
on Windows).

Darshan Rivka Whittle
- 32,989
- 7
- 91
- 109
-
Thanks for the suggestions. but .. . I will test further, however, initially, it works in Editor (r22416), but not from command line. From command line (Win7), the program just terminates with nothing showing as output or input. Obviously terminal input is/will be important to some applications, so the "best" solution would be good to know. That's my initial observation. – Brian Oh May 07 '13 at 10:34
-
@BrianOh I just tested this on Windows 7, and it worked fine with Dart 0.5.5.0_r22416, which looks like the same version that didn't work for you. I ran in with `dart.exe io.dart` and it worked perfectly. How are you launching it? – Darshan Rivka Whittle May 07 '13 at 17:06
-
Sorry, how do I get out of it without ctl-d etc.? eg. new StringDecoder().bind(stdin).listen((String sInput){}); – Brian Oh May 07 '13 at 20:19
-
@BrianOh I updated my answer to show how to do that, but then I noticed you asked it as a [separate question](http://stackoverflow.com/questions/16428292/console-application-stringdecoder-stdin), which is probably best. – Darshan Rivka Whittle May 07 '13 at 21:19
2
import "dart:async";
import "dart:io";
void main() {
print("Do you want to say something?");
Stream<String> input = stdin.transform(new StringDecoder());
StreamSubscription sub;
sub = input.listen((user_input) {
print("Really? \"${user_input.trim()}\"? That's all you have to say?");
sub.cancel();
});
}
Which example did you find, and how exactly was it wrong?

MarioP
- 3,752
- 1
- 23
- 32
-
Thanks, that eventually worked after : a) I'm on Win7, and the code did not initially work ("Readfile failed 8"). b) new Dart Editor must have come out overnight (r22416), c) Dart Editor couldn't find Java, d) Installation of Java required re-install of Java which deleted Java but didn't install it, e) Java then installed, f) then you example worked with latest Dart. I'll give it a tick. – Brian Oh May 07 '13 at 04:55
-
It's not a bug; when you create the closure and declare `sub` in the same statement, `sub` doesn't exist yet when the right-hand side is evaluated and the closure is created. By declaring `sub` first, the closure is able to close over it. – Darshan Rivka Whittle May 07 '13 at 06:10
-
@DarshanComputing Removed that part. Shame on the editor for not marking this with a warning. – MarioP May 07 '13 at 08:43
-
Death to editors! If one wants to say something, aren't we the best judge of whether it's worth consideration? – Brian Oh May 07 '13 at 11:24