Skip to content

Commit a51b7c4

Browse files
committed
♻️ Refactoring common.dart: better cancelling
1 parent d2031db commit a51b7c4

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

lib/src/common.dart

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,20 @@ Stream<R> toStreamWithTransform<T extends Listenable, R>(
1414
controller.onListen = () {
1515
assert(listener == null);
1616
try {
17-
listenable
18-
.addListener(listener = () => controller.add(transform(listenable)));
17+
final l = () => controller.add(transform(listenable));
18+
listenable.addListener(l);
19+
listener = l;
1920
} catch (_ /*Ignore*/) {
2021
controller.close();
2122
}
2223
};
2324

2425
controller.onCancel = () {
25-
assert(listener != null);
26+
if (listener == null) {
27+
// addListener thrown error.
28+
return;
29+
}
30+
2631
try {
2732
listenable.removeListener(listener!);
2833
listener = null;

0 commit comments

Comments
 (0)