[英]How to solve error The operator [] isn't Defined for the type 'object'
static void likeTweet(String currentUserId, Tweet tweet) { DocumentReference tweetDocProfile = tweetsRef.doc(tweet.authorId).collection('userTweets').doc(tweet.id); 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}); });
tweetDocProfile.get().then((doc) { int likes = doc.data()['likes']; //error the operator is not defined for the type 'object, tweetDocProfile.update({'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);静态无效与推文(字符串 currentUserId,推文推文){ 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}); });
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());
} }
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>如果您想要整个对象,则需要将其转换为 Map<String, dynamic>
int likes = (doc.data() as Map<String, dynamic>)['likes'];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.