Flutter部件状态不更新,如何解决?

Flutter的部件不会随着状态更新!!!请帮帮我
这里面的icon不会随按钮点击切换

for (var item in _categorizeImagesResult["content"]) {
        print(item["liked"]);
        categoryCardList.add(
            Container(
              child: Card(
                  clipBehavior: Clip.hardEdge,
                  child: InkWell(
                      onTap: () { },
                      child: Stack(
                        children: [
                          ...
                          Positioned(
                              bottom: 6,
                              right: 6,
                              child: IconButton(
                                  onPressed: () {
                                    int index = _categorizeImagesResult["content"].indexOf(item);
                                    if (index != -1) {
                                      setState(() {
                                        // 直接更新_categorizeImagesResult中的liked状态
                                        _categorizeImagesResult["content"][index]["liked"] =
                                        !_categorizeImagesResult["content"][index]["liked"];
                                      });
                                    }
                                  },
                                  icon: Icon(item["liked"] ? Icons.favorite : Icons.favorite_outline)
                              )
                          ),
                        ],
                      )
                  )
              ),
            )
        );
      }

代码是写在build里面的,调试时即使print(item["liked"])的值是true按钮也是按false显示??

阅读 465
avatarAI BotBETA
这个问题通常是由于在 `build` 方法中直接修改了状态数据 `_categorizeImagesResult`,但是 Flutter 框架并没有检测到这个变化,因为 `_categorizeImagesResult` 的引用没有改变。尽管你修改了列表中的某个对象的属性,但列表本身的引用仍然相同,这可能导致 Flutter 的状态管理机制(特别是 `setState`)无法正确触发界面更新。

为了解决这个问题,你可以在 `setState` 中创建一个新的列表副本,并在这个副本上修改数据。这样可以确保列表的引用发生变化,从而触发界面更新。下面是修改后的代码示例:

for (var item in List.from(_categorizeImagesResult["content"])) {

print(item["liked"]);
categoryCardList.add(
    Container(
      child: Card(
          clipBehavior: Clip.hardEdge,
          child: InkWell(
              onTap: () {},
              child: Stack(
                children: [
                  SizedBox(
                    child: BlurHash(
                      imageFit: BoxFit.cover,
                      curve: Curves.bounceInOut,
                      hash: item["blurHash"],
                      image: item["imageUrl"],
                    ),
                  ),
                  Positioned(
                      bottom: 6,
                      right: 6,
                      child: IconButton(
                          onPressed: () {
                            int index = _categorizeImagesResult["content"].indexOf(item);
                            if (index != -1) {
                              setState(() {
                                // 创建一个新的列表副本并修改其中的数据
                                List<Map<String, dynamic>> newContent = 
                                  List.from(_categorizeImagesResult["content"]);
                                newContent[index]["liked"] = 
                                  !newContent[index]["liked"];
                                // 更新原始状态数据为新的列表副本
                                _categorizeImagesResult["content"] = newContent;
                              });
                            }
                          },
                          icon: Icon(item["liked"] ? Icons.favorite : Icons.favorite_outline)
                      )
                  ),
                ],
              )
          )
    ),
)

);


在这个修改中,我使用了 `List.from()` 来创建 `_categorizeImagesResult["content"]` 的一个浅拷贝。然后,在这个拷贝上修改 `liked` 状态,并将修改后的列表重新赋值给 `_categorizeImagesResult["content"]`。这样,`setState` 就能正确检测到列表引用的变化,并触发界面更新。
1 个回答

从build方法开始的打印日历也列出来更好
复写列表的内容

 setState(() {
                     
                                List<Map<String, dynamic>> newContent = 
                                  List.from(_categorizeImagesResult["content"]);
                                newContent[index]["liked"] = 
                                  !newContent[index]["liked"];
                                _categorizeImagesResult["content"] = newContent;
                              });
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏