首页 > 解决方案 > 如何使用流生成器检查 Firebase 文档的长度

问题描述

在我的颤振 firebase 应用程序中,我可以使用查询快照获取用户在 firebase 中的活动文档的长度。但是,我希望在用户无需刷新页面的情况下实时更新文档数量。我可以通过使用流生成器转换下面的代码来获得实时长度来做到这一点吗?我该怎么做?这是我现在使用的代码,它运行良好,但不会实时更新。

 //this is the code I want to convert to stream 
    //builder.
  static Future<int> numActivities(String userId) 
     async {
     QuerySnapshot userActivitiesSnapshot = await 
      activitiesRef
    .document(userId)
    .collection('userActivities')
    .where('seen', isEqualTo: '')
    .getDocuments();
    return userActivitiesSnapshot.documents.length;
    }

标签: firebasefluttergoogle-cloud-firestoregoogle-cloud-functions

解决方案


您需要使用“返回包含docs”的属性,如下所示:ListDocumentSnapshot

return userActivitiesSnapshot.docs.length;

推荐阅读