Since you're using a StringInputStream
rather than just a standard InputStream
, and because you're looking to read text. Unless there's a particular reason, I would recommend using the onLine
handler over the onData
. On data will basically try to 'stream' the information in that it's called immediately not on a new line itself. Try something like the following (note, not complete code, missing proper error handling etc.)
#import('dart:io');
main() {
var stream = new StringInputStream(stdin);
stream.onLine = () {
var str = stream.readLine().trim();
print(str.toUpperCase());
if(str == 'EXIT') exit(0);
};
}
One other note to point out, if you ever are data-streaming and using the onData
handler, it is recommended that you then use the read
method, as opposed to the readLine
method to retrieve your content, again due to the nature of onData not waiting for a full line of text to be received before being called.