简体   繁体   English

如何解决错误 The operator [] is not defined for the type 'object'

[英]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.

相关问题 如何修复 flutter 中的“Future isn't a type”错误? - How can I fix "Future isn't a type" error in flutter? 我无法解决此MPAndroidChart空对象引用错误 - I can't solve this MPAndroidChart null object reference error 访问 VBA:声明数据库对象的类型时出错:未定义用户定义的类型 - Access VBA: Error when declaring the type of a Database object: User defined type is not defined 当我的表上没有任何nvarchar时,如何解决“将nvarchar数据类型转换为数值错误”的问题? - How can i solve “error converting data type nvarchar to numeric” when i don't have any nvarchar on my table? 这种类型的数据库如何解决? - How to solve this type of database? 未定义命名参数“xxx” - The named parameter 'xxx' isn't defined 如何解决这个试图获取Laravel中非对象错误的属性 - How To Solve This Trying to get property of non-object Error In Laravel System.InvalidCastException:没有为“DBNull”类型和“Integer”类型定义“运算符”。 - System.InvalidCastException: 'Operator '+' is not defined for type 'DBNull' and type 'Integer'.' 目标C错误:“找不到数据库”。 怎么解决? - Objective C error: “can't locate the database”. How to solve? 如何解决此约束错误 - How to solve this Constraints error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM