0

i have data stucture like:

/Admin/Dashboard/MRmZh9mixve1CV3M0EI8wfyDnb82/20220607/Anonymous/Entry

i want to read all that data that is under

/Admin/Dashboard/MRmZh9mixve1CV3M0EI8wfyDnb82

i tried this buts it returns empty

String collection = "/Admin/Dashboard/MRmZh9mixve1CV3M0EI8wfyDnb82";

await FirebaseFirestore.instance.collection(collection).get().then(
      (res) => print("Successfully completed ${res}"),
  onError: (e) => print("Error completing: $e"),
);

so basically i want lo read all the collections in it that has further documents in each collection

enter image description here

enter image description here

Farhan Fida
  • 172
  • 3
  • 12
  • None of your code returns any value, so "it returns empty" is a bit hard to help with. Which of the two `print` statements (if any) executes, and what does it output exactly? – Frank van Puffelen Jun 07 '22 at 23:10
  • the output is [] – Farhan Fida Jun 09 '22 at 09:34
  • The code accesses `/Admin/Dashboard/MRmZh9mixve1CV3M0EI8wfyDnb82`, but the screenshot shows `/Analytics/MRmZh9mixve1CV3M0EI8wfyDnb82/20220608/cburkholder`. Are these supposed to be different? – Frank van Puffelen Jun 09 '22 at 12:00
  • i updated the path and was able to read the list of documents....is there a way to get a list of all the collections? it may or may not include all data in it. it's very easy in real-time database however i cant seem to find a way in firestore. – Farhan Fida Jun 09 '22 at 16:55
  • 1
    Search is your friend here: https://www.google.com/search?q=firebase+get+a+list+of+all+the+collections suggests that this is only possible in server-side code that uses the Admin SDKs, not in client-side code such as Flutter. – Frank van Puffelen Jun 09 '22 at 19:48
  • Thanks a bunch Frank. so it means i need to write a cloud function for this ? – Farhan Fida Jun 09 '22 at 20:30
  • Well... I'd first recommend looking at your data model to see if there's a way to not need to list collections, but only list documents. Having well-known collection names (either because they're hard-coded such as "Users", "Admin" and "Dashboard"), or because their known because of the context in another way, would remove the need for querying a list of collection names. – Frank van Puffelen Jun 10 '22 at 12:21
  • Everything inside the date is unknown... even the fields of the documents (i.e. SMS OTP) is unknown... their might be maps in there too – Farhan Fida Jun 10 '22 at 15:44
  • 20220606 looks like a date to me. I'd usually expect those to be documents in a collection with a known name. But alternatively, you could also store an array of all dates that you have data for in the parent document, and use that to then determine the collections. – Frank van Puffelen Jun 10 '22 at 18:43

1 Answers1

0

I could use this way to extract data from specific documents and collections.

final _firestore = FirebaseFirestore.instance;

  void getMessages() async{
    final docRef = await _firestore.collection('1234@123.com').doc('123123').collection('record').get();
    for (var Ref in docRef.docs) {
      print(Ref.data());
    }
  }

enter image description here enter image description here


  void getMessages44() async{
    final docRef = await _firestore.collection('Admin').doc('Dashboard').collection('MRmZh9mixve1CV3M0EI8wfyDnb82').get();
    for (var Ref in docRef.docs) {
      print(Ref.data());
    }
  }

  void test() async {
    print("test");
    // List<dynamic> userList = [];
    QuerySnapshot querySnapshot = await _firestore.collection('1234@123.com').doc('123123').collection('record').where('Item', isEqualTo:"sdfsf").get();
    // userList = querySnapshot.docs.map((e) => dynamic.User.fromMap(e.data())).toList();
    final allData = querySnapshot.docs.map((doc) => doc.data()).toList();
    // final allData = querySnapshot.docs.map((doc) => doc.get('fieldName')).toList();
    print(allData);
  }
@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Record'),
        leading: null,
        actions: <Widget>[
          IconButton(
            icon: const Icon(Icons.add),
            onPressed: (){
              Navigator.pushNamed(context, creationCategory.id);
            },
          ),
          IconButton(
            icon: const Icon(Icons.settings),
            onPressed: () async {
              // await getData();
              // getMessages();
              // getMessages();
              getMessages44();
              test();
              // getMessages33();
              // Navigator.push(
              //   context,
              //   MaterialPageRoute(builder: (context) => const ItemDetailsScrrent()),
              // );
            },
          ),
        ],
      ),
      body: SafeArea(
        child: Container(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Text('Trahs Collection',
              style: const TextStyle(
                color: Colors.black38, fontWeight: FontWeight.bold, fontSize: 40),
              ),
              ItemStream(),
            ],
          ),
          ),
      ),
      );

  }

enter image description here enter image description here

Frederic Chang
  • 509
  • 1
  • 5
  • 13
  • it is still returning empty – Farhan Fida Jun 07 '22 at 22:19
  • final docRef = await _firestore.collection('Admin').doc('Dashboard').collection('MRmZh9mixve1CV3M0EI8wfyDnb82').get(); print(docRef.size); for (var Ref in docRef.docs) { print(Ref.data()); } this is what is did – Farhan Fida Jun 07 '22 at 22:20
  • 1
    Hi Farhan, I tested your collection and document, it works. See my updated comment. – Frederic Chang Jun 08 '22 at 18:28
  • Hello Frederic. your solutions pointed in me the right direction and I'm able to read all the data in the collection. is there a way to get a list of all the collections? it may or may not include all data in it. it's very easy in real-time database however i cant seem to find a way in firestore. i have updated the question. please check that out thank you. – Farhan Fida Jun 09 '22 at 09:48
  • 1
    [link1](https://stackoverflow.com/questions/70184560/getting-number-documents-of-a-collection-in-firebase-in-dart-flutter) [link2](https://stackoverflow.com/questions/46554091/cloud-firestore-collection-count/49407570#49407570) Actually It doesn't support to read all information from all of the documents. You need to know which collections and documents, you would like to read first. There're two nice links for you. you will get better understanding. – Frederic Chang Jun 12 '22 at 06:45