How can I do stdin.close() with the new Streams API in Dart?

 ̄綄美尐妖づ 提交于 2020-01-05 07:41:30

问题


I ask user for input in my command line (dart:io) app. After I get my answer from the user, I want to unsubscribe from the Stream. Then, later, I may want to listen to it again (but with different listener, so pause() and resume() don't help me).

On startup, I have this:

cmdLine = stdin
          .transform(new StringDecoder());

Later, when I want to gather input:

cmdLineSubscription = cmdLine.listen((String line) {
  try {
    int optionNumber = int.parse(line);
    if (optionNumber >= 1 && optionNumber <= choiceList.length) {
      cmdLineSubscription.cancel();
      completer.complete(choiceList[optionNumber - 1].hash);
    } else {
      throw new FormatException("Number outside the range.");
    }
  } on FormatException catch (e) {
    print("Input a number between 1 and ${choiceList.length}, please.");
  }
});

This works as intended, but it leaves stdin open at the end of program execution. With the previous API, closing stdin was as easy as calling stdin.close(). But with the new API, stdin is a Stream, and those don't have the close method.

I think that what I'm doing is closing (read: unsubscribing from) the transformed stream, but leaving the raw (stdin) stream open.

Am I correct? If so, how can I close the underlying stdin stream on program exit?


回答1:


To close stdin, you just unsubscribe from it:

cmdLineSubscription.cancel();

This is the equivalent way of doing it. So your intuition was right. I'm not sure if I understood the question -- was there a problem with this approach?



来源:https://stackoverflow.com/questions/15186821/how-can-i-do-stdin-close-with-the-new-streams-api-in-dart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!