Dart: Do I have to cancel Stream subscriptions and close StreamSinks?

后端 未结 2 1446
感动是毒
感动是毒 2021-02-02 08:28

I know I have to cancel Stream Subscriptions when I no longer want to receive any events. Do I have to this even after I receive a \'Done\' event? Or do I get memor

2条回答
  •  孤城傲影
    2021-02-02 08:50

    I found in my case that if I have code like this:

    Stream readHackerNews(String path) {  
      StreamController controller = StreamController();
    
      ......
    
      return controller.stream;
    }
    

    I see a warning message "Close instance of 'dart.core.Sink'." in the Visual Studio Code. In order to fix this warning I added

    controller.close()
    

    to the event handler for the OnCancel event, see below:

    Stream readHackerNews(String path) {  
      StreamController controller = StreamController();
    
      //TODO: your code here
    
      controller.onCancel = () {
        controller.close();
      };
    
      return controller.stream;
    }
    

    Hope this helps!

提交回复
热议问题