Search code examples
firebaseflutterdartgoogle-cloud-firestorecross-platform

How Can I Retrieve these data from Firestore?


Firestore Collection

I want to get data from the map in firestore and display it in the List ordered by date?


Solution

  • That seems to be a single document. You would have to fetch the whole document if you are just going to have a new map for each date. Then sort it yourself using dart.

    FirebaseFirestore.instance
        .collection('BMI')
        .doc('docID')
        .get()
        .then((DocumentSnapshot documentSnapshot) {
          if (documentSnapshot.exists) {
            print('Document exists on the database');
            // sort here
    
            List myList = documentSnapshot.data!.data().entries.map((entry) => entry).toList();
            print(myList);
            // sorting the list
            print(myList.sort((a, b) => a.date.compareTo(b.date)))
          }
        });
    

    You can read more about sorting lists here. If you need to limit number of entries to be fetched then you would have create a dedicated documents for each entry.