1

The Dart project I'm working on requires multiple instances print statements without new lines. As I understand that printing without a new line in dart can be achieved via

stdout.write("string");

All is fine but when I'm trying to write code similar to this:

stdout.write("Enter a number: ");
var a = stdin.readLineSync(encoding: SYSTEM_ENCODING);
stdout.write("Enter another number: ");
var b = stdin.readLineSync(encoding: SYSTEM_ENCODING);
stdout.write(a + b);

This is running perfectly on non Windows systems but in Windows (32 and 64 bits) the second message I'm printing is not showing up and the program is directly going to the stdin.readLineSync() call and waiting for input. After I give the input the message is shown. I came across similar posts here saying that stdout.write() may be async but what's weird is that this code runs perfectly fine and in synchronous manner on Linux.

Synchronous execution is very much needed in my project and the above problem can be solved via Futures but will lose the sync execution.

Any help is much appreciated.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
Rahul De
  • 393
  • 3
  • 14

2 Answers2

0

Have you tried stdout.flush() before stdin.readLineSync() ?

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • tried this: stdout.write("Enter a number: "); stdout.flush(); var a = stdin.readLineSync(encoding: SYSTEM_ENCODING); stdout.write("Enter another number: "); stdout.flush(); var b = stdin.readLineSync(encoding: SYSTEM_ENCODING); stdout.write(a + b); Its giving this error: Unhandled exception: Bad state: StreamSink is bound to a stream on the second stdout.write() – Rahul De Feb 21 '14 at 09:18
  • also as far as i know stdout.flush() returns a Future right? I would then have to write my subsequent code in a .then() block right? That kind of defeats my purpose of having a Sync Execution. – Rahul De Feb 21 '14 at 09:20
  • Yes, it's async. I guess it's better to get comfortable with async execution because sync doesn't have broad support in Dart. – Günter Zöchbauer Feb 21 '14 at 09:23
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – RononDex Feb 21 '14 at 09:29
  • @RononDex you are sure that this is no answer? Maybe it is. – Günter Zöchbauer Feb 21 '14 at 09:31
  • Your "answer" ends with a question mark, so yes I am sure – RononDex Feb 21 '14 at 09:33
  • 2
    @RononDex: that is just a weird way of putting the intent of "try this". But "have you tried this" can still be an answer. Gunter, please stick to formatting your answers as statements though. – László Papp Feb 23 '14 at 06:50
0

Stdout does not flush until a new-line character, on many platforms. I think this is true for Windows as well.

Currently the API does not expose a way to force a fflush on stdout.

I would recommend that you file a feature request at http://dartbug.com/new.

Anders Johnsen
  • 616
  • 3
  • 7