Я использую Flutter для создания приложения, в котором есть страница сообщений с категориями. Но когда я меняю категории и setState для обновления страницы, это не работает! Некоторые сообщения не обновляются, но некоторые обновляются. Мне нужно перенаправить другую страницу, а затем вернуться на страницу публикации, чтобы изменить все сообщения. Итак, initState обновляет мою страницу, а setState - нет. У меня ничего нет внутри моей страницы initState of Post.
Это мой метод создания сообщений. Работает очень хорошо. У меня есть этот метод для всех категорий, у которых есть другой метод where
.
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());
Тогда это проблема, страница сообщений;
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
}
},
)
Во всех моих категориях есть детектор жестов и setState внутри;
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)),
)),
),
],
),
)),
);
}
}