简体   繁体   English

如何保存用户“喜欢”帖子的 state?

[英]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.

相关问题 在使用 reactjs 发送 POST 请求之前,我如何等待 firebase 检查用户是否有效? - how can i wait for firebase to check the user is valid before sending a POST request with reactjs? 如何从 State 机器向 SNS 主题发送通知? - How can I send a notification from State machine to SNS topic? Firebase Firestore iOS:如何将图像保存到 Firestore 中的集合中? - Firebase Firestore iOS: How can I save images to a collection in the firestore? 如何将我的回复保存到 BASH 的列表中? - How can I save my response into a list in BASH? 如何在 flutter 中使用 getX 保存 var 的 state? - How to save the state of a var using getX in flutter? 如何加载以前登录的用户? - How can I load the previous signed in user? 如何确保用户在没有任何用户身份验证的情况下只能在我的 ReactJS 应用程序上投票一次? - How can I Ensure a User can only Vote Once on my ReactJS App Without any User Authentication? 如何创建 firebase 用户帐户 - How can I create firebase user account 在EJS文件中插入数据后,如何使用EJS创建一个HTML并保存在本地? - How can I use EJS to, after I insert the data on the EJS file, create a HTML and save it locally? 如何打印登录用户创建的所有文档字段 - How Can I print all docs fields that created by logged in user
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM