StreamBuilder limitation StreamBuilder limitation dart dart

StreamBuilder limitation


It seems like the actual problem based on your comments above is that it crashes after you navigate away from the view using the stream. You have to either:

  • Cancel your stream controller when you navigate away so that it's not listening for any more events.
  • Or just don't emit any new values through the stream after navigation. Add a pause on it until you come back to the view

Update: Adding code with pseudo example

class Widget {  // Your local stream   Stream<String> _localStream;  // Value to indicate if you have navigated away  bool hasNavigated = false;  ...  void init() {    // subscribe to the firebase stream    firebaseStream...listen((value){      // If this value is still false then emit the same value to the localStream      if(!hasNavigated) {        _localStream.add(value);      }    });  }  Widget build() {    return StreamBuilder(      // subscribe to the local stream NOT the firebase stream      stream: _localStream,      // handle the same way as you were before      builder: (context, snapshot) {         return YourWidgets();      }    );  }}


Try breaking everything into widgets

Running the query should cache it even if you fully close your app(I believe only cache it on fully closed for up to 30 minutes but if you remain without internet connection, you still have access to past previous cached queries from Firestore)

Try something like this:

  @override  Widget build(BuildContext context) {    return Scaffold(      appBar: AppBar(title: Text('Please work')),      body: _buildStream(context),    );  }  Widget _buildStream(BuildContext context) {     return StreamBuilder(      stream: yourFireStoreStream,      builder: (context, snapshot) {        if (!snapshot.hasData) return LinearProgressIndicator();        return _buildAnotherwidget(context, snapshot.data.documents);      },    );  }  Widget _buildAnotherwidget(Buildcontext context, List<DocumentSnapshot> snaps){    return ListView.Builder(      itemCount: snaps.length,      itemBuilder:(context, index) {      ..dostuff here...display your cards etc..or build another widget to display cards         }     ); }

focus on the breaking into more widgets. The highest part should have the streambuilder along with the stream. then go deep down into more widgets. The streambuilder automatically will listen and subscribe to the given stream.

When streambuilder updates, it will update the lower widgets.

Now this way, when you tap on a card in a lower widget to navigate, it should not affect the highest widget because it will effect only the UI.

we placed the streambuilder in its own top level widget...

I hope i made some sense :(

I wrote the code out without testing but im sure you can get it to work