如何更新来自不同小部件的数据播放
我得到了 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.
When a click on the post, I go into its details. And I can like the post on this screen.
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SETSTATE在这里是仅适用于此窗口小部件的本地,不会重建主页,
一个简单的解决方案是在
等待Navigator.pushnamed
之后尝试调用setState
,该将调用
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 theawait Navigator.pushNamed
which will call
setState
for the home page after we close the post pagethis 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 changeand 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