How to use FieldValue.Increment property to add a Single Value Flutter

Viewed 65

I have a post list which there is a Post Likes where user can likes the post double clicking on the picture:

GestureDetector(
                        onDoubleTap: (){
                          FirebaseFirestore.instance.collection('SocialPost').doc(post[index]['postID']).update({
                            'likes': FieldValue.increment(1),
                          });
                        },

but of course if the user clicks many times on the picture the number will increase according to the times he clicks.. I want that the user just when he click the number increase by 1 and stop if he clicks again no more action. how to handle this situation?

1 Answers

There are a few different ways to have this you could have a users collection for each user and in the documents you could store the posts the user liked or

In your SocialPost's collection you could store the ids of users that liked this post and there before you increment the value, you can check if the user already liked this post (So the user ids array contains the current user's id)

Only if the array does not contain this id you would allow it, and then after incrementing you would add the user's id to the array.

Related