我在取消使用Stream.periodic构造函数创建的流时遇到问题。下面是我取消流的尝试。但是,我很难从内部作用域中提取出'count‘变量。因此,我不能取消订阅。
import 'dart:async';
void main() {
int count = 0;
final Stream newsStream = new Stream.periodic(Duration(seconds: 2), (_) {
return _;
});
StreamSubscription mySubscribedStream = newsStream.map((e) {
count = e;
print(count);
return 'stuff $e';
}).listen((e) {
print(e);
});
// count = 0 here because count is scoped inside mySubscribedStream
// How do I extract out 'count', so I can cancel the stream?
if (count > 5) {
mySubscribedStream.cancel();
mySubscribedStream = null;
}
}
https://stackoverflow.com/questions/51583042
复制相似问题