Flutter - 无法从 firestore 获取集合

Flutter - Can't get collection from firestore

    _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);
    }
.....

我真正需要的是把最后的文档保存下来;但在此之前,我只是想获取“锻炼”集合; workouts.docs 列表总是有 0 个项目。数据库中有 2 个项目。这段代码有什么问题?还有如何获取最后保存的项目?

正如弗兰克所说:

You can refer Alex answer :

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 };
    });
});