Dart/WebStorm "Cancel instances of dart.async.StreamSubscription"

Viewed 4265

I am using WenStorm 2018.2.5 (registered), with the Dart plugin.

While editing a simple code, the editor shows me a message:

Cancel instances of dart.async.StreamSubscription

enter image description here

I don't understand what this message means. I don't see what's wrong with the code. And, if I run the code inspection, the result is : "No suspicious code found".

Any idea ?

Note :

const int eventsCount = 5;
const int waitBetweenTwoEvents = 1;

typedef Streamer = Stream<int> Function();

...

Streamer streamCreator = () async* {
  for(int i=0; i<eventsCount; i++) {
    yield i;
    sleep(Duration(seconds: waitBetweenTwoEvents));
  }
};
2 Answers

This is shown when you have the linter rule enabled in analysis_options.yaml

linter:
  rules:
    - cancel_subscriptions

Either

  • add subscription.cancel() somewhere in that class.
  • remove the linter rule
  • add a comment // ignore: cancel_subscriptions the line before or at the end of the line that shows the warning
  • add a comment // ignore_for_file: cancel_subscriptions somewhere in the file

As others have pointed out, you have a lint that requires you to cancel subscriptions.

In this case, I would just not create a subscription at all. Stream subscriptions are great when you want to control the stream by pausing or cancelling it early, or handling errors. Here you just want to do something for each data event. That's what the Stream.forEach method does. It even returns a future that is completed when the stream is done or has errored, so you don't need to do subscription.asFuture.

Related