console - How do I simulate blocking on StringInputStream readLine in Dart? -


i found answer being able read console here: is possible read console in dart?. however, want block further execution in program until string typed in (think simple console interaction user).

however, i'm not seeing way control execution flow simple interaction. realize dart i/o intended asynchronous, i'm struggling figure out how should accomplish seemingly simple task. i'm trying use dart not intended do?

#import("dart:io");  void main() {   var yourname;   var yourage;    var console = new stringinputstream(stdin);    print("please enter name? ");   console.online = () {     yourname = console.readline();     print("hello $yourname");   };    // rest of doesn't work...   print("please enter age? ");   console.online = () {      yourage = console.readline();     print("you $yourage years old");   };    print("hello $yourname, $yourage years old today!"); } 

i hope in future, io done via futures. in meantime, have 2 options:

  1. use callbacks, write in question. futures aren't way deal asynchronous execution. i'm in fact kinda surprised ask, because question contains answer -- move code little bit:

    void main() {   var stream = new stringinputstream(stdin);    stream.online = () {      var mystring = stream.readline();     print("this after handler $mystring");      // want wait until mystring typed in      print("echo $mystring"); // should not print null   };  } 
  2. write own wrapper returns future. (warning: didn't test it!):

    class mystringinputstream {   final stringinputstream delegate;    mystringinputstream(inputstream stream)       : delegate = new stringinputstream(stream);    future<string> readline() {     final completer = new completer<string>();      delegate.online = () {       final line = delegate.readline();       delegate.online = null;       completer.complete(line);     };      return completer.future;   } } 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -