Firebase 絕對正確地完成了它的作業。我是編碼和 Firebase 的新手,所以雖然錯誤訊息很清楚,但我無法弄清楚如何更正我的代碼。我收到以下錯誤訊息。我已經搜索過 SO 并嘗試了我能想到的一切但沒有成功。
流以狀態關閉:狀態{code=NOT_FOUND,描述=沒有要更新的檔案:projects/improve-practice-app/databases/(默認)/documents/posts/postID,cause=null}。
當用戶點擊帖子的圖片時,如果他們的用戶 ID 不在喜歡串列中,我想將他們的用戶 ID 添加到 Firestore 的喜歡串列中。如果他們的用戶 ID 已經在喜歡串列中,我想將其從喜歡串列中洗掉。在下面的代碼中,我嘗試使用 UUID 包更新/創建具有唯一檔案 ID 的“帖子”集合。
class FirestoreMethods {
final FirebaseFirestore _firestoreDB = FirebaseFirestore.instance;
Future<void> likePost(
{required String postID,
required String? uid,
required List likes}) async {
try {
if (likes.contains(uid)) {
await _firestoreDB.collection('posts').doc('postID').update({
'likes': FieldValue.arrayRemove([uid]),
});
} else {
await _firestoreDB.collection('posts').doc('postID').update({
'likes': FieldValue.arrayUnion([uid]),
});
}
} catch (err) {
err.toString();
}
}
}
GestureDetector(
onDoubleTap: () async {
await FirestoreMethods().likePost(
postID: widget.snap?['postID'],
uid: appUser?.uid as String,
likes: widget.snap?['likes'],
);
setState(() {
isLikeAnimating = true;
});
},

如何修復錯誤,以便為每個帖子分配一個唯一的“postID”,然后訪問“喜歡”串列?提前感謝您的幫助。
uj5u.com熱心網友回復:
如果這個答案太簡單了,我很抱歉,但是......
await _firestoreDB.collection('posts').doc('postID').update({
'likes': FieldValue.arrayRemove([uid]),
});
您正在嘗試更新名為“postID”的檔案...
我猜你想寫:
await _firestoreDB.collection('posts').doc(postID).update({
'likes': FieldValue.arrayRemove([uid]),
});
如果 postID 周圍沒有引號......傳遞變數 postID,而不是字串“postID”。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/420280.html
標籤:
