【发布时间】:2021-03-02 17:11:47
【问题描述】:
我正在使用 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)),
)),
),
],
),
)),
);
}
}
【问题讨论】:
标签: flutter flutter-layout flutter-dependencies setstate page-refresh