1

我有以下 Firestore 结构和一种从数据库获取用户提要的方法。

我需要像这样链接我的流

  1. 首先来自 User/FeedIDs 集合的所有提要 ID

  2. 然后对于每个 feedID,获取 feed 详细信息的文档并返回到它们的列表。

我可以找到解决此问题的方法,因为toList() 它不起作用或我做错了什么。

// User Collection
- User
   - RandomDocumentID
      - Feed
         - FeedIDasDocumentID
           - field1
           - field2
             .
             .

// Feed Collection
- Feed
   - RandomDocumentID
      - field1
      - field2
        .
        .

// Method in my repository to get feed for User
Observable<Feed> getCurrentUserFeed(String uid) {
    return Observable(Firestore.instance
          .collection('User')
          .document(uid)
          .collection("FeedIDs")
          .snapshots()
          .expand((snapshots) => snapshots.documents)
          .map((document) => UserFeed.fromMap(document.data))
        )
        .flatMap((userFeed) => Firestore.instance
                               .collection("Feed")
                               .document(userFeed.id)
                               .snapshots()
        )
        .map((document) => Feed.fromMap(document.data));
        // ????
        // I tried to put .toList() and of the stream but it is not working, 
       // i wanna return List<Feed> instead of every single feed object
  }


// in my BLoC
// I had to do that because I could acquire to get streams elements as a list
// 
List<Feed> feedList = List();
FirebaseUser user = await _feedRepository.getFirebaseUser();
_feedRepository.getCurrentUserFeed(user.uid).listen((feed) {
    feedList.add(feed);
    dispatch(UserFeedResultEvent(feedList));
 };

如果有任何其他链接方法,将非常感谢分享。谢谢

4

1 回答 1

8

我认为这里的问题Firestore是设置为在记录更改时发送更新。当您查询snapshots它是一个永远不会发送完成事件的流时,因为新的更新总是会进来。

如果流不发送 done 事件,则 Stream 上的某些返回 Future 的方法将永远不会完成。这些包括.single.toList()。您可能正在寻找.first在通过流发送第一个事件(数据库中记录的当前状态)并停止侦听更改后哪个将完成。

于 2019-01-04T00:23:45.283 回答