avoid-unassigned-stream-subscriptions
preset: recommended
Warns when a stream subscription is not assigned to a variable.
Not assigning a stream subscription to a variable and not calling the cancel
method when the subscription is not longer needed can lead to a memory leak.
Example
❌ Bad:
void fn() {
final stream = Stream.fromIterable([1, 2, 3]);
// LINT: This stream subscription is not assigned to a variable and may cause a memory leak.
stream.listen((event) {
print(event);
});
}
✅ Good:
void fn() {
final stream = Stream.fromIterable([1, 2, 3]);
final savedSubscription = stream.listen((event) {
print(event);
});
...
savedSubscription.cancel(); // Correct, assigned and later disposed
}