【问题标题】:Merge Firestore streams using rxDart使用 rxDart 合并 Firestore 流
【发布时间】:2019-02-13 18:31:10
【问题描述】:

我正在尝试使用 RxDart 将 Firestore 中的两个流合并为一个流,但它只返回一个流的结果

Stream getData() {
    Stream stream1 = Firestore.instance.collection('test').where('type', isEqualTo: 'type1').snapshots();
    Stream stream2 = Firestore.instance.collection('test').where('type', isEqualTo: 'type2').snapshots();
    return Observable.merge(([stream2, stream1]));
}

【问题讨论】:

标签: firebase stream dart flutter rxdart


【解决方案1】:

根据您的用例,您可能不需要 RxDart 来执行此操作。 如果您只想将两个 Firestore 流合并到一个 Dart Stream,您可以使用 dart:async 包中的 StreamZip

import 'dart:async';     

Stream<List<QuerySnapshot>> getData() {
  Stream stream1 = Firestore.instance.collection('test').where('type', isEqualTo: 'type1').snapshots();
  Stream stream2 = Firestore.instance.collection('test').where('type', isEqualTo: 'type2').snapshots();
  return StreamZip([stream1, stream2]).asBroadcastStream();
}

【讨论】:

    【解决方案2】:

    您可以使用 mergeWith 从两个单独的流中获取一个流,使用 RxDart,如下所示,

     Stream getData() {
        Stream stream1 = Firestore.instance.collection('test').where('type', isEqualTo: 'type1').snapshots();
        Stream stream2 = Firestore.instance.collection('test').where('type', isEqualTo: 'type2').snapshots();
        return stream1.mergeWith([stream2]);
      }
    

    【讨论】:

    • 请问我怎样才能访问这些数据
    猜你喜欢
    • 2020-03-25
    • 2019-08-12
    • 2020-11-01
    • 1970-01-01
    • 2020-02-28
    • 1970-01-01
    • 2020-01-26
    • 2019-04-16
    • 2023-03-26
    相关资源
    最近更新 更多