Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Console Application - terminal input

Tags:

dart

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.

like image 529
Brian Oh Avatar asked Sep 11 '26 02:09

Brian Oh


2 Answers

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

like image 79
Darshan Rivka Whittle Avatar answered Sep 18 '26 09:09

Darshan Rivka Whittle


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?

like image 22
MarioP Avatar answered Sep 18 '26 10:09

MarioP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!