How do I simulate blocking on StringInputStream readLine in Dart?

console, dart

Solution

(Answering my own question)

Dart has added several features since this question was originally asked, specifically, the concept of readLineSync using stdin. This tutorial covers several of the typical topics you might need to be aware of if writing a command-line Dart app: https://www.dartlang.org/docs/tutorials/cmdline/

import "dart:io";

void main() {
    stdout.writeln('Please enter your name? ');
    String yourName = stdin.readLineSync();
    stdout.writeln('Hello $yourName');

    stdout.writeln('Please enter your age? ');
    String yourAge = stdin.readLineSync();
    stdout.writeln('You are $yourAge years old');

    stdout.writeln('Hello $yourName, you are $yourAge years old today!');
}

Problem

I found the answer for being able to read from the console here: Is it possible to read from console in Dart?. However, I want to block further execution in my program until the string is typed in (think just simple console interaction with the user). However, I'm not seeing a way to control the execution flow for simple interaction. I realize that Dart I/O is intended to be asynchronous, so I'm struggling to figure out how I should accomplish this seemingly simple task. Is it just that I'm trying to use Dart for something that it was not intended to do? ``` #import("dart:io"); void main() { var yourName; var yourAge; var console = new StringInputStream(stdin); print("Please enter your name? "); console.onLine = () { yourName = console.readLine(); print("Hello $yourName"); }; // obviously the rest of this doesn't work... print("Please enter your age? "); console.onLine = () { yourAge = console.readLine(); print("You are $yourAge years old"); }; print("Hello $yourName, you are $yourAge years old today!"); } ```

Original source

Related problems