如何 return 流中的对象列表

How to return list of object in a stream on flutter

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

我需要像

一样链接我的流
  1. 第一个来自 User/FeedIDs 集合的所有 Feed ID

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

我可以找到解决这个问题的方法,因为 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));
 };

如果有任何其他链接方法,将不胜感激分享。谢谢

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

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