Flutter How to pass single data from Stream to another Screen and also got updated

1,544

I don't see exactly the problem but passing down the asyncSnapshot to the details page is not very useful as it may lose the reference to the first streamBuilder builder function it came from.

So the best way would be to create a stream (best it would be a BehaviorSubject from the rxDart lib) and then add to it with the data you want to pass and then pass that stream to the Details page.

Once the update finishes, the firebaseResponse should either return the new data or u manually refresh it and at that time pass it down to the Details widget via the same stream.

i can see a point in trying to pass down the same stream to the details page and save on request times (if u have the data necessary to show the items list already) but it is very messy in my opinion, u probably should take another approach, the one i mentioned or an other much simpler one.

Share:
1,544
Jolzal
Author by

Jolzal

Updated on December 26, 2022

Comments

  • Jolzal
    Jolzal over 1 year

    I was making an app with a streambuilder that listen to firestore snapshot, and creating a list of item from it, and when i click one of item it would navigate me to new screen with the detail of the item and i could also modify some info of the item there, the problem arise when i tried to update the info of item in firestore, the first screen with streambuilder got rebuild with updated data while the detail page didnt get updated, the flow is more or less like this :

    enter image description here

    and here is my simplified code:

    First Screen where i navigate and pass the asyncsnapshot and index to detail screen

    class _SalesOrderPenyediaState extends State<SalesOrderPenyedia> {
    
    Widget itemTile(context, SalesOrder element, AsyncSnapshot orderSnap, int index) {  
                            //NAVIGATE TO DETAIL SCREEN
                            GestureDetector(
                              onTap: () {
                                Navigator.of(context).push(MaterialPageRoute(
                                  builder: (context) => StreamBuilder<List<SalesOrder>>(
                                    stream: orderStreams,
                                    builder: (context, snapshot) {
                                      return SalesOrderDetailPenyedia(
                                              streamOrder: orderSnap,
                                              index: index,
                                            );
                                    }
                                  ),
                                  fullscreenDialog: true,
                                );}
                               child : Container()}
    
    
    @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: StreamBuilder(
                    stream: orderStreams,
                    builder: (context, AsyncSnapshot<List<SalesOrder>> snapshot) {
                            ListView(
                                children: snapshot.data
                                    .asMap()
                                    .map((index, item) => MapEntry(
                                        index, itemTile(context, item, snapshot, index)))
                                    .values
                                    .toList())
                      }
                    }),
              ),
            ],
          ),
    

    Detail Page

    class SalesOrderDetailPenyedia extends StatefulWidget {
      AsyncSnapshot<List<SalesOrder>> streamOrder;
      int index;
      SalesOrderDetailPenyedia({ this.index, this.streamOrder});
    
      @override
      State<StatefulWidget> createState() => SalesOrderDetailState();
      
    }
    
    class SalesOrderDetailState extends State<SalesOrderDetailPenyedia>{
      SalesOrder salesOrder;
      OrderServices orderServices = OrderServices();
     
      @override
      Widget build(BuildContext context) {
    
        salesOrder = widget.streamOrder.data[widget.index];
        return Scaffold(
        body : Column()//where i show and update my data
    )        
    }
    

    I am new to flutter and been stuck to this for a few day, any advice will be really appreciated

  • Jolzal
    Jolzal about 3 years
    hey actually it works but not without rxdart for me i just create another stream for the streambuilder for detail page and getting the data in specified index, but i dont know if this really the way since by creating 2 stream wouldnt it increase firestore read? and does index really consistent here considering there could be new data inserted