Can same stream(QuerySnapshot) be used with multiple StreamBuilder on different pages? ---Flutter

667

It happens because the stream has listeners already (the first StreamBuilder). Instead, you could use some sort of state management (it will vary on the application complexity) and provide the stream.

Let's say you are listening to a Stream<List<int>> (to make an easy example). If you used the flutter_bloc package, you could have a BLoC with a StreamSubscription which would be the only listener to that Stream and then you could consume the data from the BLoC all around your app.
It may be kind of confusing if you have little knowledge about Reactive Programming and Dart Streams (I must admit, I've lost many hours trying to understand it - yet it is simpler than it seams!), but the fluter_bloc package has a great documentation. You can find an example in which it uses the StreamSubscription with Firestore!

If you have any doubt with the package or any of the examples, feel free to post an issue in the GitHub repository!

Share:
667
Manish
Author by

Manish

Updated on December 23, 2022

Comments

  • Manish
    Manish 11 months

    Maybe it's a newbie question, but I would like to know is it possible to use same stream(Stream) to build the UI of two pages. Let's take an example: if there are two pages which should display the same list of data(say username and address) which is read from firebase. Can this be done by reading the data from firestore once and using same stream with streambuilder on each page?

    I'm able to display the list(using streambuilder and Listview.builder) on first page but when I navigate to second page and use same stream(not reading this from firebase again but using the same stream which was read for first page) it doesn't work. I tried StreamController.broadcast(); also but no luck.

    Can anyone help here?

  • marsho
    marsho over 1 year
    I think it is wrong it doesn`t share stream connection it`s just shared function
  • marsho
    marsho over 1 year
    The example you put is basically different with this case. That is more like Bloc to Bloc Stream connection
  • Rajendra A Verma
    Rajendra A Verma over 1 year
    @marsho I don't know why it worked but it did.