Skip to main content

avoid-unassigned-stream-subscriptions

added in: 1.13.0
preset: recommended

Warns when a stream subscription is not assigned to a variable.

Not disposing stream subscriptions may lead to a memory leak.

Example

❌ Bad:

void fn() {
final stream = Stream.fromIterable([1, 2, 3]);

// LINT
stream.listen((event) {
print(event);
});
}

✅ Good:

void fn() {
final stream = Stream.fromIterable([1, 2, 3]);

final savedSubscription = stream.listen((event) {
print(event);
});

...

savedSubscription.cancel();
}