尝试合并流,但仅接收到一个流

时间:2020-09-07 23:10:05

标签: firebase flutter dart google-cloud-firestore

我正在尝试使用Firebase Firestore中的查询获取数据,并且由于我在whereIn中有10多个项目,因此我必须将其作为块来处理。我遇到的问题是,我可以将数据获取为Stream并将Stream添加到List<Stream<T>>变量中,但是每当我尝试将这些流作为单个流返回时,我只能获取一个流。

这就是我试图获取多个流并将其作为单个流返回的方式;

Stream<List<Show>> getWatchNextListForUser({@required UserFull userFull}) {
    List<Stream<List<Show>>> streams = List<Stream<List<Show>>>();
    List<int> showIDs = new List<int>();

    int rounds = (userFull.myShows.length / 10).ceil();

    userFull.myShows.forEach((key, value) {
      showIDs.add(int.parse(key));
    });

    for (int i = 0; i < rounds; i++) {
      int maxSupportedRange = i == rounds - 1 ? showIDs.length % 10 : 10;
      log("i = $i & adding $maxSupportedRange");
      streams.add(_firestore
          .collection("shows")
          .where("ids.trakt",
              whereIn:
                  showIDs.getRange(i * 10, i * 10 + maxSupportedRange).toList())
          .snapshots()
          .map((snapshot) => snapshot.docs.map((document) {
                return Show.fromJson(document.data());
              }).toList(growable: true)));
    }

    // in here I can actually see that streams that have been added to the streams
    // are valid and contain data

    return StreamGroup.merge(streams);
  }

这就是我试图在前端获取这些流的方式;

    import 'package:async/async.dart' show StreamGroup;

    return StreamBuilder(
        stream: InternalQueries()
            .getWatchNextListForUser(userFull: _userFull),
        builder: (context, AsyncSnapshot<List<Show>> showList) {
            log(showList.length.toString()); // this is somethimes 5 and sometimes 10 depending on the
                                             // one stream's content
        },
    );

我有时会得到第一个流,有时会得到第二个流,等等。无论流是否被更改,流都不会更新。我该如何解决这个问题?

0 个答案:

没有答案