[英]how can I save the state of the 'liked' post by the user?
I have two collections in firestore, 'users' and 'posts'.我在 firestore 中有两个 collections,“用户”和“帖子”。 HomePage is where all the posts are displayed in a listview and every post has a 'like' button.主页是所有帖子都显示在列表视图中的地方,每个帖子都有一个“喜欢”按钮。 I'm saving the liked posts in a set final _likedPosts = Set<Posts>();
我将喜欢的帖子保存在一组final _likedPosts = Set<Posts>();
on the page but it only temporarily saves the liked posts and it loses all that data once the app is closed.在页面上,但它只是暂时保存喜欢的帖子,一旦应用程序关闭,它就会丢失所有数据。 How can I save the user's _likedPosts permanently so that the data is retained.如何永久保存用户的 _likedPosts 以便保留数据。 What query should I make for the users to retain the _likedPosts?我应该为用户保留 _likedPosts 做什么样的查询? or is there any other way for this?或者还有其他方法吗?
This is how the Icon and onTap is currently,这就是 Icon 和 onTap 目前的样子,
final _likedPosts = _savedPosts.contains(post);
Icon(_likedPosts ? Icons.favorite : Icons.favorite_border,
color: _likedPosts ? Colors.red : null),
onTap: () {
setState(() {
if (_likedPosts) {
_savedPosts.remove(post);
} else {
_savedPosts.add(post);
}
});
}
Are you saving a liked post of a certain user?您是否正在保存某个用户的点赞帖子? then I suggest getting that post(ID) and save it to an array in the users doc Liked-Posts per user.然后我建议获取该帖子(ID)并将其保存到每个用户的用户文档 Liked-Posts 中的数组中。 Because state doesn't persist or can't be saved unless you use an external db.因为 state 不会持续存在或无法保存,除非您使用外部数据库。
you have to create this item like class StatefulWidget你必须像 class StatefulWidget 这样创建这个项目
class MyLikeButton extends StatefulWidget { \\...
and after add to state class of this item add "with AutomaticKeepAliveClientMixin" like this:并在添加到此项的 state class 后添加“with AutomaticKeepAliveClientMixin”,如下所示:
class _MyLikeButtonState extends State<MyLikeButton> with AutomaticKeepAliveClientMixin {
@override
bool get wantKeepAlive => true; \\....
this will keep alive the changes in the item when scrolling, its that u want?这将使滚动时项目中的更改保持活动状态,这是你想要的吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.