Flutter - 流不返回数据

发布于 2025-01-13 21:50:09 字数 1694 浏览 2 评论 0原文

我有一个名为 posts_favorites 的文档集合,它存储对用户已添加书签的所有帖子的引用。 posts_favorites 集合如下所示:

在此处输入图像描述

我创建了一个流来获取属于特定用户的所有帖子引用,然后我想使用帖子引用从帖子集合中获取帖子文档。

我创建了一个 Stream 来生成我需要的数据,但我没有从 Stream 返回任何数据。这是我的 Stream 代码:

  Stream<List<PostsRecord>> getFavoritePostsStream() async* {

List<PostsRecord> myList = [];

await FirebaseFirestore.instance
    .collection("posts_favorites")
    .where("user", isEqualTo: currentUserReference)
    .get()
    .then((favoriteList)  {

        favoriteList.docs.forEach((element) async {

          String documentPath = element['post'].toString();
          var start = documentPath.indexOf("/");
          var end = documentPath.indexOf(")");
          var documentRef = documentPath.substring(start+1, end);

          //DocumentReference docRef = FirebaseFirestore.instance.doc(documentPath);

          DocumentReference docRef = FirebaseFirestore.instance.collection('posts').doc(documentRef);
          await docRef.get().then((DocumentSnapshot documentSnapshot) async {
            if (documentSnapshot.exists) {
              print('Document exists on the database');
              PostsRecord postsRecord = PostsRecord.getDocumentFromData(
                  documentSnapshot.data(), element['post']);
              //return myList.add(postsRecord);
              //print(postsRecord);
              return postsRecord;
            }
          });
        });
    });
}

​​我知道这个函数可以工作,因为注释的代码会生成我希望从 Firestore 获取的发布记录。

我不确定如何让 Stream 函数返回可以在 Stream 中使用的数据。

谢谢

I have a document collection called posts_favorites, which stores the reference to all the Posts that a user has bookmarked. The posts_favorites collection look as follows:

enter image description here

I have created a Stream to get all posts references that belong to a specific user, then I want to get the Post documents from the posts collection using the posts references.

I created a Stream to produce the data I need, but I am not getting any data returned from my Stream. Here is my code for the Stream:

  Stream<List<PostsRecord>> getFavoritePostsStream() async* {

List<PostsRecord> myList = [];

await FirebaseFirestore.instance
    .collection("posts_favorites")
    .where("user", isEqualTo: currentUserReference)
    .get()
    .then((favoriteList)  {

        favoriteList.docs.forEach((element) async {

          String documentPath = element['post'].toString();
          var start = documentPath.indexOf("/");
          var end = documentPath.indexOf(")");
          var documentRef = documentPath.substring(start+1, end);

          //DocumentReference docRef = FirebaseFirestore.instance.doc(documentPath);

          DocumentReference docRef = FirebaseFirestore.instance.collection('posts').doc(documentRef);
          await docRef.get().then((DocumentSnapshot documentSnapshot) async {
            if (documentSnapshot.exists) {
              print('Document exists on the database');
              PostsRecord postsRecord = PostsRecord.getDocumentFromData(
                  documentSnapshot.data(), element['post']);
              //return myList.add(postsRecord);
              //print(postsRecord);
              return postsRecord;
            }
          });
        });
    });
}

I know this function works because the commented code produces the Post Records that I expect to get from Firestore.

I am not sure how to get the Stream function to return data that I can use in the Stream.

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文