简体   繁体   English

Dart超时后如何取消订阅原始流?

[英]How to cancel subscription of the original stream after timeout in Dart?

I have a broadcast stream that I subscribe to it with the timeout:我有一个广播流,我用超时订阅它:

final s = originBroadcast.timeout(timeout, onTimeout: (sink) => sink.close());
await for (final event in s) {
  ...
}

The problem is that timeout() creates another stream ( s ), and when I unsubscribe or timeout appeared, it doesn't cancel the original stream ( originBroadcast ).问题是timeout()创建了另一个流( s ),当我取消订阅或出现超时时,它不会取消原始流( originBroadcast )。 Is it possible to do this?是否有可能做到这一点?

I found a workaround:我找到了解决方法:

final s = originBroadcast.timeout(
  timeout,
  onTimeout: (sink) => sink.close(),
);
s.listen((event) {...});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM