我想停止收听快照更新。即使关闭屏幕,快照仍会继续监听更新。我正在使用下面的代码来监听更新。
CollectionReference reference = Firestore.instance.collection('Events'); reference.snapshots().listen((querySnapshot) { querySnapshot.documentChanges.forEach((change) { // Do something with change }); })
您的监听器类型为StreamSubscription,因此您可以在监听器上调用一些有用的方法,例如cancel()
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();