【发布时间】:2019-07-20 20:20:36
【问题描述】:
在我的 Flutter 应用程序中,StreamSubscription 没有暂停或取消。当我打电话给cancel() 时,如果它之前开始,它会停止。如果我在启动后调用cancel(),它不会停止。我正在使用 Firestore 快照侦听器。下面是我的代码。
我尝试了不同的方法,但仍然无法正常工作。问题是 Firestore listener 在加载数据后没有停止。
StreamSubscription<QuerySnapshot> streamSubscription;
@override
void initState() {
super.initState();
print("Creating a streamSubscription...");
streamSubscription =Firestore.collection("name").document("d1").collection("d1")
.snapshots().listen((data){
//It will display items
}, onDone: () { // Not excecuting
print("Task Done");
}, onError: (error) {
print("Some Error");
});
streamSubscription.cancel(); //It will work but cancel stream before loading
}
@override
void dispose() {
streamSubscription.cancel(); //Not working
super.dispose();
}
【问题讨论】:
-
stream.close()应该是stream.cancel()并且StreamSubscription应该被称为streamSubscription或subscription而不是stream因为这很令人困惑,因为Stream完全是另外一回事. -
api.dartlang.org/stable/2.2.0/dart-async/… 中没有
close(),所以它不起作用也就不足为奇了。如果您收到错误消息,您应该将该错误消息与您的问题一起发布。 -
我已经编辑了我的问题。我正在使用“取消()”未关闭。很抱歉发生了错误。
-
@GünterZöchbauer 你知道它为什么没有停止吗?
-
很难说。可能是因为事件是同步发出的,因此在调用
cancel()之前发出。您的问题对实际发生的事情以及预期的行为不够清楚。如果你能改进它,那么你得到有用答案的可能性就会增加。
标签: firebase firebase-realtime-database dart flutter google-cloud-firestore