Flutter
i have this listen function into stful
which i need to close it when press on the back button
class Test extends StatefulWidget {
const Test({Key key}) : super(key: key);
@override
_TestState createState() => _TestState();
}
class _TestState extends State<Test> {
handleDelete(){
FirebaseFirestore.instance.collection("handleCountM").limit(1).snapshots().listen((value) {
value.docs.forEach((element) {
element.reference.delete();
});
});
}
@override
Widget build(BuildContext context) {
return Container();
}
}
i have no idea how could i stop it into dispose()
Stream<QuerySnapshot<Map<String, dynamic>>> myStream = FirebaseFirestore.instance.collection("handleCountM").limit(1).snapshots();
late StreamSubscription<QuerySnapshot<Map<String, dynamic>>> streamSubscription;
void handleDelete() {
streamSubscription = myStream.listen((value) {
value.docs.forEach((element) {
element.reference.delete();
});
});
}
@override
void dispose() {
streamSubscription.cancel(); //Cancel your subscription here.
super.dispose();
}
Your other alternative, would be to use a streambuilder, and it'll handle the subscription and termination for you.