Hey folks,
I'm kind of baffled by this. I've done google searches and saw a thread on GitHub about something similar (which is where I sot the idea to use stdin.listen().cancel();, but I'm getting ahead of myself).
I haven't worked much with Streams, mostly Subscriptions and Futures in AngularDart,
I have a scenario where I have a command-line tool and I prompt the user for input and I have a timeout on that request largely due to unit tests failing if I don't find someway to end the prompt.
I am using stdin to wait for input from the user, a simply yes/no or y/n.
But when I run my tests, the first test fails at the end I have stdin.listen().cancel(); because the stream is "has already been listened to". If I remove this, the tests work fine, locally. But I feel like I should close the stream when I no longer need it.
############### Example ################
import 'dart:convert';
import 'dart:io';
String _userDecision = '';
final _inputStream = utf8.decoder.bind(stdin);
Future<void> _promptUser() async {
_userDecision = await _inputStream
.timeout(const Duration(seconds: 15), onTimeout: (sink) => sink.add('N'))
.first;
_userDecision = _userDecision.toLowerCase();
if (_userDecision.isNotEmpty) {
// Closing Stream once userDecision has a proper value
await _inputStream.listen((event) => event).cancel();
stdout.writeln('Stream closed');
}
}
##################