Flutter 颤振设置状态未正确刷新页面

Flutter 颤振设置状态未正确刷新页面,flutter,flutter-layout,flutter-dependencies,setstate,page-refresh,Flutter,Flutter Layout,Flutter Dependencies,Setstate,Page Refresh,我正在使用flatter构建一个应用程序,它有一个带有类别的帖子页面。 但是当我更改类别并设置状态以刷新页面时,它不起作用!有些帖子并没有刷新,但有些帖子刷新了。我需要路由另一个页面,然后返回帖子页面以更改所有帖子。因此initState正在刷新我的页面,但setState没有。我的帖子页面的初始状态中没有任何内容 这是我创造帖子的方法。它工作得很好。我的所有类别都有这个方法,其中有另一个where方法 Firestore.instance .collection("p

我正在使用flatter构建一个应用程序,它有一个带有类别的帖子页面。 但是当我更改类别并设置状态以刷新页面时,它不起作用!有些帖子并没有刷新,但有些帖子刷新了。我需要路由另一个页面,然后返回帖子页面以更改所有帖子。因此initState正在刷新我的页面,但setState没有。我的帖子页面的初始状态中没有任何内容

这是我创造帖子的方法。它工作得很好。我的所有类别都有这个方法,其中有另一个
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
                  } 
                },
              )

我的所有类别都有一个手势检测器和设置状态

 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)),
                  )),
            ),
          
          ],
        ),
      )),
    );
  }
}




将statefull小部件包装在类别小部件周围,或使用Statefullbuilder包装

如果你问我,我建议你使用Statefullbuilder,因为你可能需要频繁地传输数据

Statefullbuilder帮助刷新底部工作表、对话框和其他小部件


我已经试过了,我的postpage类也是statefull。你是否也包装了categories小部件…不仅仅是使用statefull小部件或statefull生成器进行postpage?正如我所说,我已经试过了。而且这不是解决方案,因为postpage已经如此。-我应该试过了吗?好的,然后让我找到其他东西…还有那就告诉你