Home > front end >  Nested replies in a comment sections - How to add new replies for Firebase Firestore
Nested replies in a comment sections - How to add new replies for Firebase Firestore

Time:09-15

Right now, to add a reply I have to do this:

final CollectionReference postsRef = FirebaseFirestore.instance
  .collection('Media')
  .doc("<docNumber>")
  .collection("Posts")
  .doc(document)
  .collection("Replies");

I want there to be a way that I could dynamically add replies collection to a document. Right now, the only way for me to do that is if I do .doc().col().doc().col().doc().col()... etc.

As you see, I would rather only have to give a single path instead of having to add many nested collections for adding the information. Is there a way I can just provide a string path?

CodePudding user response:

You can provide a path like this:

final CollectionReference postsRef = FirebaseFirestore.instance
  .collection('Media/${docID}/Posts/${docID2}/Replies')

A CollectionReference has odd and a DocumentReference has even number of path segments. So if you want to point to a document, it'll be:

.doc('Media/${docID}/Posts/${docID2}/Replies/${docID3}')
  • Related