I am using Futurebuilder to update my profile data but it is not showing new products added until I restart my app.
FutureBuilder(
future: FirebaseFirestore.instance
.collection('posts')
.where('uid', isEqualTo: widget.uid)
.get(),
builder: (context, AsyncSnapshot snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: CircularProgressIndicator(),
);
}
return GridView.builder(
shrinkWrap: true,
itemCount: snapshot.data!.docs.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
crossAxisSpacing: 5,
mainAxisSpacing: 2,
childAspectRatio: 1,
),
........
CodePudding user response:
FutureBuilder is basically used to get data once your app is built. It does not provide you with continuous updates of data until you rebuild your app. To get continuous data you can use StreamBuilder. For more information, you can go through the flutter documentation. https://api.flutter.dev/flutter/widgets/StreamBuilder-class.html
Here is the code that will help you out.
StreamBuilder(
stream: FirebaseFirestore.instance
.collection('posts')
.where('uid', isEqualTo: widget.uid)
.snapshot(),
builder: (context, AsyncSnapshot snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: CircularProgressIndicator(),
);
}
return GridView.builder(
shrinkWrap: true,
itemCount: snapshot.data!.docs.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
crossAxisSpacing: 5,
mainAxisSpacing: 2,
childAspectRatio: 1,
),
........