'How to delete a specific array object that is inside a map in firebase? (Unlike functionality) (Flutter)
I am trying to implement a like and unlike functionality on my project. If I unlike a post, I want it to delete only the current user from the firebase. But the main problem is that when I unlike the post, it empties the whole array instead of removing only the user that unliked the post. It works perfectly fine if there is only one like but the problem occurs if there are two users who have liked the post.
This is what I implemented on the screen. The like functionality works perfectly fine so you can ignore that part. The 'user.username' refers to the current user.
IconButton(onPressed: () async {
// if(dat.userId != uid) {
if(dat.likeData.usernames.contains(user.username)){
ref.read(crudProvider).removelike(dat.id, user.username, dat.likeData.like);
} else {
final likes = Like(
usernames: [...dat.likeData.usernames, user.username],
like: dat.likeData.like + 1
);
ref.read(crudProvider).addlike(likes, dat.id);
}
This is the remove Like provider. I've tried FieldRemove.arrayRemove but it isn't working at all. The number of like does decreases if we unlike the post. The main issue is the 'usernames' array.
enter coFuture<void> removelike(String postId, String username, int like) async {
try{
await dbPosts.doc(postId).update({
'likes' : {
'like' : like - 1,
'usernames' : FieldValue.arrayRemove([username])
}
});
} on FirebaseException catch (err) {
print(err);
}
}
This is the 'posts' collection in the Firebase. For example, the post has 2 likes and If I unlike it then the value of 'like' turns into 1 but the 'usernames' array which used to consist two usernames who had liked the post will become empty.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

