Brian Oh
Brian Oh

Reputation: 10720

Console Application - terminal input

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.

Upvotes: 3

Views: 635

Answers (2)

Darshan Rivka Whittle
Darshan Rivka Whittle

Reputation: 34031

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).

Upvotes: 4

MarioP
MarioP

Reputation: 3832

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?

Upvotes: 3

Related Questions