【问题标题】:Flutter setState is not refresh page rightlyFlutter setState 没有正确刷新页面
【发布时间】: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


    【解决方案1】:

    在您的类别小部件周围包裹一个有状态的小部件或使用 Statefullbuilder 包裹它。

    如果你问我,我会推荐你​​使用 Statefullbuilder,因为你可能需要经常传输数据。

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

    关注statefullbuilder

    【讨论】:

    • 我已经尝试过了,而且我的 postpage 类也是有状态的。
    • 您是否也包装了类别小部件...不仅仅是带有状态小部件或状态构建器的帖子?
    • 正如我所说,我已经尝试过了。这也不是解决方案,因为 postpage 已经如此了。-我已经尝试过了-
    • 好的,那我找点别的……然后告诉你
    猜你喜欢
    • 2020-12-05
    • 2022-07-23
    • 1970-01-01
    • 1970-01-01
    • 2018-06-11
    • 2019-06-07
    • 1970-01-01
    • 2021-10-28
    • 2020-06-12
    相关资源
    最近更新 更多