3

ここでコンソールから読み取ることができることに対する答えを見つけました: Is it possible to read from console in Dart? . ただし、文字列が入力されるまで、プログラムでのそれ以上の実行をブロックしたいと考えています (ユーザーとの単純なコンソール操作を考えてみてください)。

ただし、単純な対話の実行フローを制御する方法がわかりません。Dart I/O は非同期であることを意図していることに気付いたので、この一見単純なタスクをどのように達成すべきかを理解するのに苦労しています。Dart を意図していないことに使用しようとしているだけですか?

#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!");
}
4

2 に答える 2

4

(私自身の質問に答える)

この質問が最初に尋ねられて以来、Dart はいくつかの機能を追加しました。具体的には、stdin を使用した readLineSync の概念です。このチュートリアルでは、コマンドライン Dart アプリを作成する場合に知っておく必要がある典型的なトピックをいくつか取り上げます: 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!');
}
于 2014-01-06T19:53:18.583 に答える