【问题标题】:Flutter using a stream within another stream使用另一个流中的流颤动
【发布时间】:2020-09-12 17:44:06
【问题描述】:

我有一个使用 firebase 作为数据库的颤振项目。我正在尝试通过流获取文档名称列表,将其分成 10 个一组,以解决 firebase 中 .where() 函数的 10 个比较最大值,然后使用 .where() 发出批处理请求并合并它们。

我知道的问题是:

  1. friends.length 不能用作循环中的数字,因为它是 future 类型

  2. Firestore 查询返回无法添加到流列表中,原因我不明白(“无法将参数类型 'Query' 分配给参数类型 'Stream'。”)

  3. .where() 期望 1 个位置参数,但我给它 3 个,尽管 Firestore 文档说我应该这样做

  4. StreamGroup 欠精细

  Stream<List<Memo>> get memos {
    // create list of streams
    List<Stream> streams = [];
    // Get list of friend userids
    var friends = Firestore.instance.collection("users").document(userid).collection("friends").snapshots().map(_snapshotToStringList);
    // split friend userid list into chunks of 10
    for (var i = 0; i < friends.length; i += 10) {
      // query database for each chunk of 10 and add to stream list
      streams.add(Firestore.instance.collection("memos").where("userid", "in", friends.sublist(i, i+10)));
    }
    // merge and return stream list
    return StreamGroup.merge(streams);
  }

感谢任何和所有建议

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore stream


    【解决方案1】:

    使用此代码:

    Stream<List<Memo>> get memos {
      List<Stream> streams = [];
      Firestore.instance
          .collection("users")
          .document(userid)
          .collection("friends")
          .snapshots()
          .map(_snapshotToStringList)
          .listen((friends){
        for (var i = 0; i < friends.length; i += 10) {
        // query database for each chunk of 10 and add to stream list
          streams.add(Firestore.instance
            .collection("memos")
            .where("userid", isEqualTo: friends.sublist(i, i+10)));
        }
      });
      return StreamGroup.merge(streams);
    }
    

    【讨论】:

    • 谢谢,这看起来比我的好多了,但似乎仍然存在问题。 stream.add 中的表达式仍然是错误的,因为不能将参数类型“Query”分配给参数类型“Stream”。我还想问一下是否需要导入才能使用 StreamGroup,因为它是未定义的。
    • api.flutter.dev/flutter/package-async_async/… 它的异步类,但我也不能使用 StreamGroup 它的未定义即使我导入异步类也不知道它可能会被删除
    • 通过导入包 pub.dev/packages/async 解决了流组问题。我上面提出的streams.add问题可以通过将列表转换为无法与StreaGroup合并的Query类型来解决。你有什么建议吗?对于所有这些问题,我深表歉意,我对 Flutter 和 Firebase 很陌生。
    • streams.add(Firestore.instance.collection("memos").where("userid", isEqualTo: friends.sublist(i, i+10)).snapshots());使用它来添加流
    猜你喜欢
    • 2020-09-29
    • 2019-12-14
    • 1970-01-01
    • 1970-01-01
    • 2021-04-07
    • 1970-01-01
    • 2021-07-16
    • 2021-04-14
    • 2020-10-09
    相关资源
    最近更新 更多