Reputation: 3451
I have a structure made up of tabs. I'm listening which tab you're in. I am using AutomaticKeepAliveClientMixin
in tabs
late StreamSubscription _subscription;
@override
void initState() {
super.initState();
_subscription = FirebaseFirestore.instance
.collection('users')
.doc(id)
.snapshots().listen((DocumentSnapshot snapshot) => _onUpdate(snapshot));
widget.tab?.addListener(_tabListener);
}
@override
void dispose() {
_subscription.cancel();
widget.tab?.removeListener(_tabListener);
super.dispose();
}
_tabListener () {
if(widget.tab?.page == 0.0) {
if(_subscription.isPaused) {
_subscription.resume();
}
} else {
_subscription.pause();
}
}
No problem so far, it stops listening and picks up where it left off, but when it starts again it brings all the changes that happened when it stopped.
Keeps sending snapshot changes constantly in the background.
I want the stream to stop completely when the tab changes, and start over when it comes to tab, how can I do that?
Upvotes: 0
Views: 203
Reputation: 419
According to the StreamSubscription pause method documentation, if the subscription receives events while paused they will be buffered until the subscription is resumed.
As explained in this paragraph the recommended solution to avoid buffering events would be to cancel the subscription and start it again when needed.
Upvotes: 1