How to solve error The operator [] isn't Defined for the type 'object'

Viewed 25

static void likeTweet(String currentUserId, Tweet tweet) { DocumentReference tweetDocProfile = tweetsRef.doc(tweet.authorId).collection('userTweets').doc(tweet.id); tweetDocProfile.get().then((doc) { int likes = doc.data()['likes']; //error the operator isn't defined for the type 'object, tweetDocProfile.update({'likes': likes + 1}); });

DocumentReference tweetDocFeed =
    feedRefs.doc(currentUserId).collection('userFeed').doc(tweet.id);
tweetDocFeed.get().then((doc) {
  if (doc.exists) {
    int likes =  doc.data()['likes'];
    tweetDocFeed.update({'likes': likes + 1});
  }
});

likesRef.doc(tweet.id).collection('tweetLikes').doc(currentUserId).set({});

addActivity(currentUserId, tweet, false, null);

}

static void unlikeTweet(String currentUserId, Tweet tweet) { DocumentReference tweetDocProfile = tweetsRef.doc(tweet.authorId).collection('userTweets').doc(tweet.id); tweetDocProfile.get().then((doc) { int likes = doc.data()['likes']; tweetDocProfile.update({'likes': likes - 1}); });

DocumentReference tweetDocFeed =
    feedRefs.doc(currentUserId).collection('userFeed').doc(tweet.id);
tweetDocFeed.get().then((doc) {
  if (doc.exists) {
    int likes = doc.data()['likes'];
    tweetDocFeed.update({'likes': likes - 1});
  }
});

likesRef
    .doc(tweet.id)
    .collection('tweetLikes')
    .doc(currentUserId)
    .get()
    .then((doc) => doc.reference.delete());

}

1 Answers

Use this instead of you want only one key:

int likes =  doc.get('likes');

If you want the entire object, you need to cast it as Map<String, dynamic>

int likes =  (doc.data() as Map<String, dynamic>)['likes'];
Related