2

I already know if I want to use stream listen in method to a snapshot so i need to use StreamSubscription then dispose it using .cancel();

but what if use streamBuilder under build method to get data from fisrstore, do i need to dispose it too if yes HOW ?.. or it will be disposed it self when I press back bottom ?

  @override
  Widget build(BuildContext context) {

          
          return StreamBuilder(
            stream: FirebaseFirestore.instance.collection("users").doc(widget.userId).snapshots(),
            builder: (context, snapshot)  {
              if (!snapshot.hasData) {
                return circulearProgress();

              }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Jack
  • 161
  • 1
  • 16

1 Answers1

4

The StreamBuilder automatically manages the lifecycle of the underlying stream you pass it. You don't need to do anything for that yourself here, it will auto-close the stream (and remove the listener from Firestore) when the widget disappears from the UI that your build method returns.

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807