I have two collections in firestore, 'users' and 'posts'. 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>(); 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. What query should I make for the users to retain the _likedPosts? or is there any other way for this?
This is how the Icon and onTap is currently,
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. Because state doesn't persist or can't be saved unless you use an external db.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With