flutter - 停止在 flutter 中收听 Cloud Firestore 中的快照更新

标签 flutter google-cloud-firestore

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

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

最佳答案

您的监听器是 StreamSubscription 类型,因此您可以在监听器上调用一些有用的方法,例如 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 - 停止在 flutter 中收听 Cloud Firestore 中的快照更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51918047/

相关文章:

firebase - 是否可以在 Flutter 中使用 FCM 实现通知分组/捆绑?

node.js - 如何在 Firestore 中正确执行按日期查询?

javascript - 从 Firebase 集合中查询数据

google-maps - 在 initState() 中调用时, flutter 的谷歌地图自定义图标不显示

flutter - Dart中类名开头的美元符号$是什么意思?

javascript - 如何使用下面的代码仅选择集合中的特定文档?

javascript - 创建数据firestore后如何返回值

firebase - 如何在呈现应用程序时从 firestore 获取集合并设置为 vuex 状态?

flutter 多个页面 View 和 Controller

android - 三星手机中的 Flutter 构建失败,Permission Denial : broadcast from android asks to run as user -1 but is calling from user 0