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
});
})
You don't have to create a new listener to unsubscribe. Every time you call onSnapshot() the return value is a handler you can invoke to unsubscribe that listener later. That snippet is meant to show two parts of your app with the "//..." indicating other code in between.
You can listen to a document with the onSnapshot() method. An initial call using the callback you provide creates a document snapshot immediately with the current contents of the single document. Then, each time the contents change, another call updates the document snapshot.
Very late answer, but I thought I'd complete the previous answers with a code sample as it might be useful to others.
class EventsScreen extends StatefulWidget {
EventsScreen({Key key}) : super(key: key);
@override
_EventsScreenState createState() => _EventsScreenState();
}
class _EventsScreenState extends State<EventsScreen> {
StreamSubscription<QuerySnapshot> _eventsSubscription;
@override
void initState() {
// Initialise your stream subscription once
CollectionReference eventsReference = Firestore.instance.collection('Events');
_eventsSubscription = eventsReference.snapshots().listen((snapshot) => _onEventsSnapshot);
super.initState();
}
@override
Widget build(BuildContext context) {
// Build your widget here
return Container();
}
void _onEventsSnapshot(QuerySnapshot snapshot) {
// Remove the setState() call if you don't want to refresh your screen whenever you get a fresh snapshot
setState(() {
snapshot?.documentChanges?.forEach(
(docChange) => {
// If you need to do something for each document change, do it here.
},
);
// Anything you might do every time you get a fresh snapshot can be done here.
});
}
@override
void dispose() {
// Cancel your subscription when the screen is disposed
_eventsSubscription?.cancel();
super.dispose();
}
}
This approach leverages the StatefulWidget's state to handle documents changes.
A better approach would be to use a Provider or the BLoC pattern so that the subscription to your Firestore is not created and handled in the UI.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With