skip to Main Content

I want to get the pregnancyInfo document ID which is a subcollection from users collection .

why I want to get the document ID? because I want to access the weight subcollection

here is a picture of the firestore

As you can see from the code below, what am doing is copying and pasting the doc id but each user have a different pregnancy info document ID. So is there a way to retrieve the Document id and store it in a variable?

 Future<Widget> getWeight(String weightDate, String weight) async {
String userUid = getUserId();
print('weight date is $weightDate');
QuerySnapshot result = await FirebaseFirestore.instance
    .collection('users')
    .doc(userUid)
    .collection('pregnancyInfo')
    .doc("HMEBTKrnOYnmxPmBMHuV")
    .collection('weight')
    .get();

2

Answers


  1. If I correctly understand your question you need to execute a query on the pregnancyInfo sub-collection under the userUid document.

    You will get one or more documents corresponding to one or more "babies".

    QuerySnapshot result = await FirebaseFirestore.instance
        .collection('users')
        .doc(userUid)
        .collection('pregnancyInfo')
        .get();
    
    Login or Signup to reply.
  2. If you want to query the pregnancy info across all users, you can use a collection group query to read from all pregnancyInfo collections at once.

    Similarly, you can use a collection group query on weight to read from all weight collections in one go.

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