I'm using Flutter to build an app which has a post page with categories. But when I change the categories and setState to refresh the page, it is not working! Some of the posts are not refreshing but someones refresh. I need to route another page and then come back to the Post page to change all posts. So initState is refreshing my page but setState is not. I have nothing inside my initState of Post page.
It's my method that creates posts. It works very well. I have this method for my all categories which have another where method.
Firestore.instance
.collection("posts")
.where("likers", arrayContains: Constants.firebaseUser.uid)
.orderBy("id", descending: true)
.getDocuments()
.then((value) => Constants.productsForOwn = value.documents
.map((documentSnapshot) =>
Product.fromDocument(documentSnapshot, Constants.position))
.toList());
Then that is the problem, posts page;
ListView.builder(
controller: controller,
itemCount: Constants.products.length + 1,
itemBuilder: (context, index) {
if (index == 0) {
// return the header
return Container(
height: MediaQuery.of(context).size.height / 6,
width: MediaQuery.of(context).size.width,
color: Colors.white,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: ListView(
scrollDirection: Axis.horizontal,
children: [
category(Colors.grey, 'Her Şey',
Icon(Icons.accessibility), 0, 'productName1'),
category(
Colors.red,
'Süt Ürünleri',
Icon(Icons.accessibility),
1,
'productname2'),
],
),
),
);
} else (Constants.products.length >= index) {
return Constants.products[index - 1]; //creating posts
}
},
)
All of my categories have a gesture detector and setState within;
Widget category(
Color color, String title, Icon icon, int index, String productType) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 6.0),
child: Container(
child: GestureDetector(
onTap: () async {
Constants.indexOfPicked = index;
Constants.searchingProductType = productType;
await Datas().assignAllPosts(); //that reassign the posts
setState(() {});
},
child: Column(
children: [
Expanded(
flex: 4,
child: AspectRatio(
aspectRatio: 1,
child: Container(
decoration: BoxDecoration(
border: Border.all(
color: (Constants.indexOfPicked == index)
? Colors.green
: Colors.transparent,
width: 2),
color: color,
borderRadius: BorderRadius.circular(12)),
)),
),
],
),
)),
);
}
}