检查StreamSubscription是否已取消

时间:2019-10-29 10:23:04

标签: flutter dart rxdart

简单点说:

如何检查StreamSubscription是否已取消?

没有

_myCustomSub.isCanceled

3 个答案:

答案 0 :(得分:0)

似乎您必须使用以下两种方法之一:

  1. onDone method-并在单独的变量中保留流是关闭还是..
  2. cancel method-和await表示信号流订阅已取消的未来。

不知道其他任何方式。

答案 1 :(得分:0)

我认为这可能是您想要的:

final streamController = StreamController();
streamController.onCancel = (){};

根据文档:Creating streams in Dart

Waiting for a subscription

  

要收到订阅通知,请在您指定onListen参数时   创建StreamController。当onListen回调在   流第一个订阅者。如果您指定onCancel回调,   当控制器失去最后一个订阅者时调用。

Final hints

  

由以下方法定义的onListen,onPause,onResume和onCancel回调   当流的侦听器时,流将调用StreamController   状态更改

此外,这可能会有所帮助:

//Whether there is a subscriber on the [Stream]
streamController.hasListener

答案 2 :(得分:0)

是的,正如其他人所说,没有办法检查流订阅是否被取消,这有点蹩脚。我猜您无权访问 StreamController,所以这是我的两个选项。

  1. _myCustomSub 设置为 null 作为取消订阅的标志并采取相应措施。

  2. 另一种方法是将 bool 值 (isSubCanceled) 与 _myCustomSub 变量放在一起,并使用它代替 _myCustomSub.isCanceled