skip to Main Content

I’m facing with an issue in the comment section of the app i’m working on (Instagram based). I have created a comment setion with a like button (that changes colours when the uid of the user is present in the DB) and I’d like to give the possibility to users to use it and save the data on FirebaseFirestore. I have created a subcollection "comments" from my "posts" collection.

Now when I link the code to the IconButton, I get this error:

W/Firestore(12560): (24.3.1) [WriteStream]: (607b719) Stream closed with status: Status{code=NOT_FOUND, description=No document to update: projects/gestigram-tut/databases/(default)/documents/comments/2a1d8040-5c57-11ed-80e6-2b7d542041dc, cause=null}.
I/flutter (12560): [cloud_firestore/not-found] Some requested document was not found.

As you can see in the code below the doc exist.

If I try to put the uid on Firebase in the commentLikes document, the IconButton Like/dislike change colour.

Can somebody help me figure out where is the issue? Thanks in advance.

Cheers.
Q

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/foundation.dart';
import 'package:gestigram/models/comment.dart';
import 'package:gestigram/models/post.dart';
import 'package:gestigram/ressources/storage_methods.dart';
import 'package:uuid/uuid.dart';
import 'package:image_picker/image_picker.dart';

class FirestoreMethods {
  final FirebaseFirestore _firestore = FirebaseFirestore.instance;

  Future<String> uploadPost(
    String description,
    Uint8List file,
    String uid,
    String username,
    String profImage,
  ) async {
    String res = "Some error occured";
    try {
      String photoUrl =
          await StorageMethods().uploadImageToStorage('posts', file, true);

      String postId = const Uuid().v1();
      Post post = Post(
        description: description,
        uid: uid,
        username: username,
        postId: postId,
        datePublished: DateTime.now(),
        postUrl: photoUrl,
        profImage: profImage,
        likes: [],
      );

      _firestore.collection('posts').doc(postId).set(
            post.toJson(),
          );
      res = 'success';
    } catch (err) {
      res = err.toString();
    }
    return res;
  }

  Future<String> uploadVideoPost(
    String description,
    Uint8List file,
    String uid,
    String username,
    String profImage,
  ) async {
    String res = "Some error occured";
    try {
      String photoUrl =
          await StorageMethods().uploadVideoToStorage('posts', file, true);

      String postId = const Uuid().v1();
      Post post = Post(
        description: description,
        uid: uid,
        username: username,
        postId: postId,
        datePublished: DateTime.now(),
        postUrl: photoUrl,
        profImage: profImage,
        likes: [],
      );

      _firestore.collection('posts').doc(postId).set(
            post.toJson(),
          );
      res = 'success';
    } catch (err) {
      res = err.toString();
    }
    return res;
  }

  Future<void> likePost(String postId, String uid, List likes) async {
    try {
      if (likes.contains(uid)) {
        await _firestore.collection('posts').doc(postId).update({
          'likes': FieldValue.arrayRemove([uid]),
        });
      } else {
        await _firestore.collection('posts').doc(postId).update({
          'likes': FieldValue.arrayUnion([uid]),
        });
      }
    } catch (e) {
      print(
        e.toString(),
      );
    }
  }

  // post comment
  Future<void> postComment(String postId, String text, String uid, String name,
      String profilePic) async {
    try {
      if (text.isNotEmpty) {
        String commentId = const Uuid().v1();
        Comment comment = Comment(
            commentId: commentId,
            commentLikes: [],
            datePublished: DateTime.now(),
            name: name,
            profilePic: profilePic,
            text: text,
            uid: uid);
        await _firestore
            .collection('posts')
            .doc(postId)
            .collection('comments')
            .doc(commentId)
            .set(
              comment.toJson(),
            );
      } else {
        print('Text is empty');
      }
    } catch (e) {
      print(
        e.toString(),
      );
    }
  }

// like comment
  Future<void> likeComment(
      String commentId, String uid, List commentLikes) async {
    try {
      if (commentLikes.contains(uid)) {
        await _firestore.collection('comments').doc(commentId).update({
          'commentLikes': FieldValue.arrayRemove([uid]),
        });
      } else {
        await _firestore.collection('comments').doc(commentId).update({
          'commentLikes': FieldValue.arrayUnion([uid]),
        });
      }
    } catch (e) {
      print(
        e.toString(),
      );
    }
  }
  //deleting posts

  Future<void> deletePost(String postId) async {
    try {
      await _firestore.collection('posts').doc(postId).delete();
    } catch (err) {
      print(err.toString());
    }
  }
// follow user

  Future<void> followUser(
    String uid,
    String followId,
  ) async {
    try {
      DocumentSnapshot snap =
          await _firestore.collection('users').doc(uid).get();
      List following = (snap.data()! as dynamic)['following'];

      if (following.contains(followId)) {
        await _firestore.collection('users').doc(followId).update({
          'followers': FieldValue.arrayRemove([uid])
        });

        await _firestore.collection('users').doc(uid).update({
          'following': FieldValue.arrayRemove([followId])
        });
      } else {
        await _firestore.collection('users').doc(followId).update({
          'followers': FieldValue.arrayUnion([uid])
        });

        await _firestore.collection('users').doc(uid).update({
          'following': FieldValue.arrayUnion([followId])
        });
      }
    } catch (e) {
      print(e.toString());
    }
  }
}

As you can see in the code below the doc exist.

If I try to put the uid on Firebase in the commentLikes document, the IconButton Like/dislike change colour.

Can somebody help me figure out where is the issue? Thanks in advance.

Cheers.
Q

2

Answers


  1. Collection Is The main one and Collection has docs also docs can have multiple collections

    try {
              FirebaseFirestore.instance
                  .collection('users')//this is collection name
                  .doc(user!.uid)//this is docid
                  .collection("course")//this is the collection tittle
                  .add({"id": courseid});//this is where your id need to add
            } on Exception catch (e) {
              Logger().e(e);
              // TODO
            }
    
    Login or Signup to reply.
  2. your like function should be look like this:

       Future<void> likeComment(String postId,
          String commentId, String uid, List commentLikes) async {
            final path = 'posts/$postId/comments/$commentId';
        try {
          if (commentLikes.contains(uid)) {
            await _firestore.doc(path).update({
              'commentLikes': FieldValue.arrayRemove([uid]),
            });
          } else {
            await _firestore.doc(path).update({
              'commentLikes': FieldValue.arrayUnion([uid]),
            });
          }
        } catch (e) {
          print(
            e.toString(),
          );
        }
      }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search