【问题标题】:Flutter Get Firestore Documents stream by list of IDsFlutter 通过 ID 列表获取 Firestore 文档流
【发布时间】:2021-01-28 13:39:42
【问题描述】:

我正在寻找一种通过另一个 ID 流获取文档流的方法,假设我有一个像 Stream<List<String>> docIds = ['3dd34334, '45454de', ...] 这样的文档 ID 流,那么我需要使用我已经拥有的 ID 查询另一个集合,请查找我下面的代码返回一个空流。


Stream<List<ViewModel>> matches(uid) async { 

  Stream<List<String>> docIds = FirebaseFirestore.instance
      .collection('matches')
      .where('match', arrayContains: uid)
      .snapshots()
      .map((event) => event.docs.map((e) => e.id).toList());

  return docIds.map((e) {
    e.map((e) => FirebaseFirestore.instance
        .collection('matches')
        .doc(e)
        .collection("myCollection")
        .snapshots()
        .map(
          (event) => event.docs.map((e) => ViewModel.fromJson(e.data())).toList(),
        ));
  }).toList();

}

【问题讨论】:

    标签: flutter google-cloud-firestore stream


    【解决方案1】:

    您可以通过多种方式创建 Streams,

    使用 yield 基本示例:

    Stream<List<int>> countStream(int to) async* {
      for (int i = 1; i <= to; i++) {
        List<int> list = List<int>();
        yield list;
      }
    }
    

    现在使用 firebase firestore 和 transforming the stream:

    Stream<List<ViewModel>> matches(uid) async* {
       Stream<List<String>> docIds = FirebaseFirestore.instance
          .collection('matches')
          .where('match', arrayContains: uid)
          .snapshots()
          .map((event) => event.docs.map((e) => e.id).toList());
       await for (List<ViewModel> chunk in source) {
           // Do other operations if you need
           yield chunk
       }
    }
    

    【讨论】:

    • 对我不起作用,您能否再解释一下您的答案,也请使用实际示例
    • 哪一部分不适合你?你有错误信息吗?我不清楚您在 Firestore 中有哪些基础数据或中间变量的内容是什么。
    猜你喜欢
    • 2021-11-19
    • 1970-01-01
    • 2021-12-16
    • 2021-07-20
    • 2019-05-05
    • 2022-07-22
    • 1970-01-01
    • 2020-12-05
    • 2018-04-23
    相关资源
    最近更新 更多