Home > Blockchain >  How can I access each document id in Firebase?
How can I access each document id in Firebase?

Time:01-07

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

CodePudding user response:

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

CodePudding user response:

Take a look in Flutter Firebase documentation.

It contains all the basic information to understand how Flutter interacts with Firebase.

Maybe later i could help more. Luck!

CodePudding user response:


Descarga texnow del play store son número reales dijitales y con correos también el problema que vas ocupar Inés para retirar el defectivo de la plataforma

  • Related