1

In my app I have two screens in which I am showing data from firestore using streambuilders and also using Navigator.push() to move between screens. When streambuilders fetches data it rebuilds its descendants and shows the data.Everything is working fine till here but when I move to second screen the streambuilder on the first screen rebuilds again.

Correct me if I am wrong here (If streambuilder is rebuilding again means it creates a new stream and fetches the data again from firestore everytime I move between screens).

If on rebuilding data is not fectched again from firestore then there is no problem but if it does then how to solve this?

code for FirstScreen

class FirstScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {

    print('FirstScreen');

    return Scaffold( 
      body: Center(
        child: InkWell(
          onTap: () => Navigator.push(context, MaterialPageRoute(builder: (_) => SecondScreen())),

          child: Container(
            child: StreamBuilder(
              stream: Firestore.instance.collection('items').snapshots(),
              builder: (context,snap){

                print('FirstScreen : ${snap.connectionState}');

                if(snap.hasData)
                return Text(snap.data.documents[0]['name']);

                else return CircularProgressIndicator();
              },
            ),
          ),
        ),
      ),
    );
  }
}

code for second screen

class SecondScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {

    print('SecondScreen');
    
    return Scaffold( 
      body: Center(
        child: Container(
          child: StreamBuilder(
            stream: Firestore.instance.collection('products').snapshots(),
            builder: (context,snap){

              print('SecondScreen : ${snap.connectionState}');

              if(snap.hasData)
              return Text(snap.data.documents[0]['name']);

              else return CircularProgressIndicator();
            },
          ),
        ),
      ),
    );
  }

when FirstScreen loads

I/flutter ( 6416): FirstScreen build
I/flutter ( 6416): FirstScreen : ConnectionState.waiting
I/flutter ( 6416): FirstScreen : ConnectionState.active

when move to SecondScreen

I/flutter ( 6416): FirstScreen build
I/flutter ( 6416): FirstScreen : ConnectionState.waiting
I/flutter ( 6416): FirstScreen : ConnectionState.active
I/flutter ( 6416): SecondScreen build
I/flutter ( 6416): SecondScreen : ConnectionState.waiting
I/flutter ( 6416): SecondScreen : ConnectionState.active
I/flutter ( 6416): FirstScreen build
I/flutter ( 6416): FirstScreen : ConnectionState.waiting
I/flutter ( 6416): FirstScreen : ConnectionState.active
Saurabh Pandey
  • 151
  • 3
  • 12
  • U can subscribe to a `stream` ,i.e, u get data when it's available. I doubt that new data can be requested from a `stream` object. In the builder add this code `if (snap.data.documentChanges.length != 0) print('new data available');`. This way you will know when the `widget` was build with new data from firestore. – Navaneeth P Aug 01 '20 at 16:00
  • But my concern here is why the stream builder is rebuilding when I move to another page and did the stream builder read the data again from firestore or just showing data fetched by it earlier? – Saurabh Pandey Aug 01 '20 at 16:38
  • 3
    You probably want to read this https://stackoverflow.com/questions/52249578/how-to-deal-with-unwanted-widget-build – Rémi Rousselet Aug 01 '20 at 17:37

0 Answers0