skip to Main Content

In my blogapp ,I am trying to implement comment system in flutter using cloud firestore. Each document is a post and I am trying to add comments as a subcollection to each document. How can I access each document id so that I can access the subcollection ?

Here I tried to print the document id below but it shows error NoSuchMethodError: 'id' method not found Receiver: null Arguments: [] :

 final ref = await FirebaseFirestore.instance
                            .collection('blogs')
                            .add({
                              'title': titleController.text,
                              'body': myController.text,
                              'author': name,
                              'date': DateFormat('dd/MM/yyyy,hh:mm')
                                  .format(DateTime.now()),
                            })
                            .then((value) => successAlert(context))
                            .catchError((error) => 
                        errorAlert(context));
                        titleController.clear();
                        myController.clear();
                        print(ref.id);
                      }

This is my firestore database :

enter image description here

2

Answers


  1. You need to have access to the document id inside blogs, and access it’s collection and the add your data like this…

    await FirebaseFirestore.instance
                                .collection('blogs')
                                .doc(documentId) // you need to enter this document id
                                .collection('comments').add({
                                  'title': titleController.text,
                                  'body': myController.text,
                                  'author': name,
                                  'date': DateFormat('dd/MM/yyyy,hh:mm')
                                      .format(DateTime.now()),
                                })
    
    
    Login or Signup to reply.
  2. When you dont have the document id Firebase provides the get() command which returns the content of a collection. Like this:

    db.collection("cities").get().then((res) => print("Successfully completed"), onError: (e) => print("Error completing: $e"));
    

    But i realize that you will need the document id, so to get this you have to store the id after inserting a post, i’ve an example from a personal project:

    await firestore.collection('item').add({
          'quantity': element.quantity,
          'product_id': element.product.productId,
        }).then((value) {
          /// Here you can access [value.id] to get this document id.
          /// Now you can save it in a list or a class instance. Just keep it.
        })
    

    Now you can use the document id to update the post with a new comment, like this. For more information we have the Firestore documentation with flutter code snipets.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search