how to fix error: 'No document to update' Firebase Flutter

I'm getting the following error message.

Stream closed with status: Status{code=NOT_FOUND, description=No document to update: projects/improve-practice-app/databases/(default)/documents/posts/postID, cause=null}.

When a user taps the image of a post, I want to add their userID to the likes list in Firestore if their userID isn't already in the likes list. And if their userID is already in the likes list I want to remove it from the likes list. In the code below I am trying to update/create a 'posts' collection with a unique document id using the UUID package.

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;
});
},

enter image description here

How do I fix the error so that I can assign a unique 'postID' to every post and then gain access to the 'likes' list? Thanks in advance for any help.


Solution 1:

I am sorry if this answer is too easy, but ...

await _firestoreDB.collection('posts').doc('postID').update({
          'likes': FieldValue.arrayRemove([uid]),
        });

You are trying to update the docment which is named "postID" ...

I guess you wanted to write:

await _firestoreDB.collection('posts').doc(postID).update({
          'likes': FieldValue.arrayRemove([uid]),
        });

Without the quotation marks around postID ... pass the variable postID, not the String "postID".