skip to Main Content
DocumentReference tweetDocProfile = tweetsRef.doc(tweet.authorId).collection('userTweets').doc(tweet.id);
tweetDocProfile.get().then((doc) {
    int likes = doc.get('likes');
    tweetDocProfile.update({'likes' : likes+1});
});

Value of likes is not updating in Firebase, I am able to update it in code but update is not working I guess

2

Answers


  1. Try this!

    DocumentReference tweetDocProfile = tweetsRef.doc(tweet.authorId).collection('userTweets').doc(tweet.id);
    tweetDocProfile.get().then((doc) { 
      int likes = doc.get('likes'); 
      tweetDocProfile.update({'likes' : int.parse(likes)+1}); 
    });
    
    Login or Signup to reply.
  2. try this way, it is safer to modify,
    It saves you a get request and the script internally uses a transaction so it doesn’t overwrite the data.

    DocumentReference userTweetRef = tweetsRef.doc(tweet.authorId).collection('userTweets').doc(tweet.id);
    userTweetRef.update({ likes: FieldValue.increment(1) });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search