本文介绍了停止在 flutter 中收听 Cloud Firestore 中的快照更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想停止收听快照更新.即使在屏幕关闭后,快照也会继续收听更新.我正在使用以下代码来收听更新.

I want to stop listening to snapshot updates. The snapshot keeps listening to updates even after the screen is closed. I am using the below code to listen to the updates.

CollectionReference reference = Firestore.instance.collection('Events');
reference.snapshots().listen((querySnapshot) {
  querySnapshot.documentChanges.forEach((change) {
    // Do something with change
  });
})

推荐答案

您的侦听器属于 StreamSubscription 类型,因此您可以在侦听器上调用一些有用的方法,例如 cancel()

Your listener is of type StreamSubscription, so you can call some helpful methods on your listener such as cancel()

    CollectionReference reference = Firestore.instance.collection('Events');
StreamSubscription<QuerySnapshot> streamSub = reference.snapshots().listen((querySnapshot) {
  querySnapshot.documentChanges.forEach((change) {
    // Do something with change
  });
});
//somewhere
streamSub.cancel();

这篇关于停止在 flutter 中收听 Cloud Firestore 中的快照更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 06:40