如何更新来自不同小部件的数据播放

发布于 2025-01-24 02:22:22 字数 1243 浏览 0 评论 0原文

我得到了 Postem 和FutureBuilder的列表。

PostItem获得了喜欢按钮和类似的计数。

“在此处输入图像说明”

单击帖子时,我会详细介绍其详细信息。我可以喜欢此屏幕上的帖子。

这是当我单击“详细屏幕”中的“喜欢”按钮时:

Future<void> _updateLike() async
  {
    PhpPost phpPost = PhpPost();
    phpPost.posteModel = widget.postModel;

    if(_isLike)
    {
      String res = await phpPost.unlikePost();
      if(res=="OK")
      {
        setState(() {
          _isLike = false;
        });
      }
    }
    else
    {
      String res = await phpPost.likePost();
      if(res=="OK")
      {
        setState(() {
          _isLike = true;
        });
      }
    }
    widget.postModel.isLike = _isLike;
  } 

屏幕详细信息很好地更新,但是当我返回主屏幕时,帖子项目未更新。

这是我从帖子项目中获取细节的方式:

Navigator.pushNamed(context, '/post_detail', arguments: widget.postModel);

I got a list of PostItem with a FutureBuilder.

PostItem got a like button and a like count.

enter image description here

When a click on the post, I go into its details. And I can like the post on this screen.

enter image description here

Here is when I click on the like button in detail screen :

Future<void> _updateLike() async
  {
    PhpPost phpPost = PhpPost();
    phpPost.posteModel = widget.postModel;

    if(_isLike)
    {
      String res = await phpPost.unlikePost();
      if(res=="OK")
      {
        setState(() {
          _isLike = false;
        });
      }
    }
    else
    {
      String res = await phpPost.likePost();
      if(res=="OK")
      {
        setState(() {
          _isLike = true;
        });
      }
    }
    widget.postModel.isLike = _isLike;
  } 

The screen detail update nicely but when I go back at the home screen the post item not updated the like.

Here is how I go to detail from post item :

Navigator.pushNamed(context, '/post_detail', arguments: widget.postModel);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼泪也成诗 2025-01-31 02:22:22

SETSTATE在这里是仅适用于此窗口小部件的本地,不会重建主页,

一个简单的解决方案是在等待Navigator.pushnamed之后尝试调用setState

该将调用setState在主页上关闭帖子页面后,

await Navigator.pushNamed(context, '/post_detail', arguments: widget.postModel);
setState((){});

如果您在构建方法中计算类似计数,则应在setState中重新计算它,这将起作用。


一个更好的解决方案是不要使用SetState完全处理用户数据更改
并使用诸如changeNotifier,Bloc或Riverpod的提供商之类的国家管理解决方案
您将拥有一个控制器,该控制器将更改数据并更新小部件

setState here is a local for only this widget and wont rebuild the home page

a simple solution is to try call setState after the await Navigator.pushNamed

which will call setState for the home page after we close the post page

await Navigator.pushNamed(context, '/post_detail', arguments: widget.postModel);
setState((){});

this will work if you are calculate the like count at the build method or you should re-calculate it inside setState


a better solution is to not use a setState at all for handling a user-data change
and use state management solution like provider with ChangeNotifier, bloc or riverpod
which you will have a controller that will change the data and update the widget

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文