A few options come to mind. First, you could use takeWhile()
to set a 'done' condition:
new StringDecoder().bind(stdin)
.takeWhile((s) => s.trim() != 'exit')
.listen((sInput) {
That will use the same onDone
handler (if one is set) when the user inputs the EOF
character or types exit
followed by the enter key. You can have more flexibility by cancelling the subscription with cancel()
:
void main() {
stdout.write("Enter Data : ");
var sub;
sub = new StringDecoder().bind(stdin).listen((String sInput) {
if (sInput.trim() == 'exit' || sInput.trim() == 'bye')
sub.cancel();
// Do something with sInput ............
});
Cancelling the subscription doesn't close the Stream, so any onDone
handler isn't called.
Of course, if you've got nothing left to do, you can always terminate with exit(0)
[1].