I'm creating a Process using Process.start and am a bit stuck with the stdin getter. Ideally, I've got a StreamController set up elsewhere, whose stream of Strings I'd like to pass into stdin. But there aren't too many sophisticated examples for interacting with Process.stdin, so I'm not sure how to do anything more than a writeln to stdin.
So I've got something like this, that I can add String messages to:
StreamController<String> processInput = new StreamController<String>.broadcast();
And I want to do something like this:
Process.start(executable, args, workingDirectory: dir.path, runInShell: true).then((Process process) {
process.stdout
.transform(UTF8.decoder)
.listen((data) {
s.add('[[CONSOLE_OUTPUT]]' + data);
});
process.stdin.addStream(input.stream);
});
I realize that addStream()
wants Stream<List<int>>
, though I'm not sure why that's the case.
This might work
The
stdin
object is anIOSink
, so it has awrite
method for strings. That will default to UTF-8 encoding the string. So, instead ofyou can do
You may want some error handling, possibly flushing stdin between writes, so maybe:
Alternatively you can do the UTF-8 encoding manually, to get the stream of list of integers that
addStream
expects:The reason why
stdin
expects aList<int>
is that process communication is, at its core, just bytes. Sending text requires the sender and the receiver to pre-agree on an encoding, so they can interpret the bytes the same way.