سؤال

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.

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top