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