In my Flutter app StreamSubscription
is not pausing or cancelling. When I call cancel()
if it started before, it will stop. If I call cancel()
after starting, it will not stop. I am using Firestore snapshot listener. Below is my code.
I have tried different methods but it's still not working. The problem is that the Firestore listener
is not stopping after loading data.
StreamSubscription<QuerySnapshot> streamSubscription;
@override
void initState() {
super.initState();
print("Creating a streamSubscription...");
streamSubscription =Firestore.collection("name").document("d1").collection("d1")
.snapshots().listen((data){
//It will display items
}, onDone: () { // Not excecuting
print("Task Done");
}, onError: (error) {
print("Some Error");
});
streamSubscription.cancel(); //It will work but cancel stream before loading
}
@override
void dispose() {
streamSubscription.cancel(); //Not working
super.dispose();
}