how to get collection list from document Id in flutter Firestore?

Viewed 1542

i want "a9XvvHPEvhfDWTXe8HqPqWuP2gg1" this documentId collection list.

     FirebaseFirestore.instance
      .collection('tbl_FriendList').doc(
      'a9XvvHPEvhfDWTXe8HqPqWuP2gg1').get().then((event) {});

enter image description here

2 Answers

You can't do that with the Flutter sdk. You'll need to restructure your data.

Retrieving a list of collections is not possible with the mobile/web client libraries. You should only look up collection names as part of administrative tasks in trusted server environments. If you find that you need this capability in the mobile/web client libraries, consider restructuring your data so that subcollection names are predictable.

https://firebase.google.com/docs/firestore/query-data/get-data#list_subcollections_of_a_document

As per you image, seems like there is no document in this ID. You only have sub-collections.

To get sub-collection you have to improve your query and you should know your sub-collection name as mentioned below

await FirebaseFirestore.instance
        .collection('tbl_FriendList')
        .doc('a9XvvHPEvhfDWTXe8HqPqWuP2gg1')
        .collection('collectionPath')
        .get();

Or you have to store data in db like this

a9XvvHPEvhfDWTXe8HqPqWuP2gg1 > collectionName > documents

FirebaseFirestore.instance
        .collection('tbl_FriendList')
        .doc('a9XvvHPEvhfDWTXe8HqPqWuP2gg1')
        .collection('collectionName')
        .add({'data': 'data123'});

after that you can get data as mentioned below

FirebaseFirestore.instance
        .collection('tbl_FriendList')
        .doc('a9XvvHPEvhfDWTXe8HqPqWuP2gg1')
        .collection('collectionName')
        .get()
        .then((value) => {});

This query will return all data you have in collectionName sub-collection.

Related