使用快照方法在颤振中获取 Firestore 问题

时间:2021-07-28 15:47:08

标签: firebase flutter google-cloud-firestore

我有一个包含当前观看电影的每个用户的 ID 的观众子集合 viewers 的电影集合。

我需要你的帮助来绑定来自 Firestore 的用户流。做到这一点:

  1. 我从子集合中获取所有查看者 ID
  2. 我使用 whereIn 创建了一个查询过滤器,以检查我当前获取的用户是否在查看者列表中 问题是我什么也拿不到。由于 snapshot() 什么都没有了。我已经使用这种方法和 map 迭代查询快照,但在这种情况下没有任何作用

希望有人能帮助我! :)

Stream<List<UserModel>> viewerStream(String movieId) {
    List<String> viewersId = [];
    List<UserModel> viewers = [];

    _firestore
        .collection(MOVIECOLLECTION)
        .doc(movieId)
        .collection(VIEWERS)
        .get().then((value) {
      value.docs.forEach((element) {
        viewersId.add(element.id);
      }); //works fine, I get my userIds

      if (viewersId == null || viewersId.length == 0) return null;

     return _firestore
          .collection(USERSCOLLECTION)
          .where(FieldPath.documentId, whereIn: viewersId)
          .snapshots()//since here, nothing works
          .map((query) {
        query.docs.forEach((element) {
          viewers.add(UserModel.fromDocumentSnapshot(documentSnapshot: element));
        });
      });
      return viewers;
    });
  }```

2 个答案:

答案 0 :(得分:0)

试试这个

    _firestore
    .collection(USERSCOLLECTION)
    .where(FieldPath.documentId, whereIn: viewersId)
    .snapshots()
    .listen((event) => onViewersChange(event.docChanges));

对于文档的每次更改

  onViewersChange(List<DocumentChange<Map<String, dynamic>>> docChanges) {
    docChanges.forEach((docChange) {
      if (docChange.type == DocumentChangeType.added) {
        docChange.doc.data(); // Add to your Viewers List
      } else if (docChange.type == DocumentChangeType.modified) {
        docChange.doc.data(); // Replace this data to the existing data in Viewers List
      } else if (docChange.type == DocumentChangeType.removed) {
        docChange.doc.data(); //Remove this data from your viewerList
      }
    });
  }

答案 1 :(得分:0)

我试过你给我看的东西,但现在我的听众总是返回空:

The method 'listen' was called on null.
Receiver: null
Tried calling: listen(Closure: (List<UserModel>) => List<UserModel>)

这是我的代码:

Stream<List<UserModel>> viewersStream(String movieId) {
    List<String> viewersId = [];
    List<UserModel> viewers = [];

    onViewersChange(List<DocumentChange> docChanges, List<UserModel> viewersList) {
      docChanges.forEach((docChange) {
        if (docChange.doc != null && docChange.doc.exists) {
          if (docChange.type == DocumentChangeType.added) {
            viewersList.add(UserModel.fromDocumentSnapshot(
                documentSnapshot: docChange.doc));
            docChange.doc.data(); // Add to your Viewers List
          } else if (docChange.type == DocumentChangeType.modified) {
            docChange.doc
                .data(); // Replace this data to the existing data in Viewers List
            viewersList.forEach((element) {
              if (element.id == docChange.doc.id) {
                element = UserModel.fromDocumentSnapshot(
                    documentSnapshot: docChange.doc);
              }
            });
          } else if (docChange.type == DocumentChangeType.removed) {
            docChange.doc.data();
            viewersList.removeWhere((element) => (element.id ==
                docChange.doc.id)); //Remove this data from your viewerList
          }
        }
      });
    }

    _firestore
        .collection(MOVIECOLLECTION)
        .doc(viewerId)
        .collection(VIEWERS)
        .get()
        .then((value) {
      value.docs.forEach((element) {
        viewersId.add(element.id);
      });

      _firestore
          .collection(USERSCOLLECTION)
          .where(FieldPath.documentId, whereIn: viewersId)
          .snapshots()
          .listen((event) {
        onViewersChange(event.docChanges, viewers);
        return viewers;
      });
    });
  }