【发布时间】:2020-04-10 08:23:48
【问题描述】:
我有一个关于如何正确制作无限数据流以停止添加到主题的问题。像下面的例子:
void main() async {
var infinite = Stream.periodic(0.5.seconds, (it) => it);
var subject = BehaviorSubject<int>();
subject.addStream(infinite);
subject.listen(print);
await Future.delayed(Duration(milliseconds: 1000));
await subject.close(); //It will not allow me to close the subject
}
infite 是一个无限的数据源。它永远不会发出close 事件。那么,如何正确关闭subject,因为它永远不会完成接收数据?
编辑:Bad state: You cannot close the subject while items are being added from addStream in flutter 上提出的解决方案不起作用仅仅是因为drain 永远不会返回。
【问题讨论】: