0
    _getLatestCompletedWorkout() async {
  try {
    QuerySnapshot workouts;
    workouts = await FirebaseFirestore.instance
        .collection('users')
        .doc(FirebaseAuth.instance.currentUser!.uid)
        .collection('workouts')
        .get();
    for (var workout in workouts.docs) {
      print('WORKOUT = ');
      print(workout);
    }
.....

What I really need is to get the last document saved; but before that I am just trying to fetch the "workouts" collection; the workouts.docs list always has 0 items. There are 2 items in the DB. What is wrong with this code? Also how to get the last saved item?

who-aditya-nawandar
  • 1,334
  • 9
  • 39
  • 89
  • 2
    For the last question, see https://stackoverflow.com/questions/54178951/get-last-created-document-in-a-firebase-firestore-collection and more from https://www.google.com/search?q=how+to+get+the+last+saved+document+from+firestore – Frank van Puffelen Dec 11 '21 at 15:55
  • @who-aditya-nawandar Posted an answer, is it helpful? – Divyani Yadav Dec 20 '21 at 11:03

2 Answers2

0

As mentioned by Frank :

You can refer Alex answer here :

The simplest way to achieve this is to add a date property to each object in your collection, then simply query it according to this new property descending and call limit(1) function.

This is the required query:

this.historyRef = afs.collection<History>('history', ref => ref.orderBy('date', 'desc').limit(1));
this.history = this.historyRef.snapshotChanges().map(actions => {
    return actions.map(a => {
        const data = a.payload.doc.data() as Hisotory;
        const docId = a.payload.doc.id;
        return { docId, ...data };
    });
});
Divyani Yadav
  • 1,030
  • 4
  • 9
0

This has been found since cloud_firestore updates that prevent app that not regiter in App Check to take updates seriously "but it store it with lighter id color" which Protect your Cloud Firestore resources from abuse, such as billing fraud or phishing

Kindly check and ensure your app is registered inside App Check in Firebase console