Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

next Page stream is not update on Flutter

Tags:

flutter

dart

I used two pages. and I added StreamBuilder in my first page and I passed snapshot.data to next Page. but when value change in 2nd-page value is not changing. How to fix it? I can't call streamBuilder in both pages because it's meaning two read in firebase. Is there any way to create singleton for services and access anywhere?

StreamBuilder(
              stream: db.getData(),
              builder: (context,snapshot){
                return Column(
                  children: <Widget>[
                    InkWell(
                      onTap: ()=> Navigator.pushNamed(context, '/nextPage',arguments: Arguments(
                        data: snapshot.data
                      )),
                    )
                  ],
                );
              },
            )
like image 233
BIS Tech Avatar asked Dec 04 '25 10:12

BIS Tech


1 Answers

InkWell(
  onTap: ()=> Navigator.pushNamed(context, '/nextPage',arguments: Arguments(
  data: snapshot.data
  ),
),

When using the above code, a data snapshot is only sent when you Tap on the InkWell. Meaning unless tapped on the inkwell it will not provide new data to nextPage.

To resolve this issue, I would suggest the following:

In First page

  • Create ValueNotifier instance to observe changes in the common reference:
  // change Cast to type that you receive from firebase, or you can omit the cast 
 final ValueNotifier<snapshot_data_type> firebaseDataNotifier = ValueNotifier<snapshot_data_type>();
  • Update the value of firebaseDataNotifier when you receive data from firebase:
 StreamBuilder(
           stream: db.getData(),
           builder: (context,snapshot){
             firebaseDataNotifier.value = snapshot.data;
             ...
  • Pass the firebaseDataNotifier as data for the nextPage

In the next Page

  • Wrap the Next page widgets with ValueListenable
  ValueListenableBuilder(
          valueListenable: valueNotifier,
          builder: (context, firebaseData, child) {
            // return your next page widget here and use firebaseData instead of snapshot.data
        )

Note: Make sure you keep the ValueNotifier instance outside of both widgets so that they can access it without any dependency.

like image 116
Harshvardhan Joshi Avatar answered Dec 06 '25 23:12

Harshvardhan Joshi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!