使用rxDart合并Firestore流 [英] Merge Firestore streams using rxDart

查看:169
本文介绍了使用rxDart合并Firestore流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to merge two streams from Firestore into one stream using RxDart, but it's only returning results of one stream

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]));
}

推荐答案

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

Depending on your use case, you might not need RxDart to do this. If you just want to have two Firestore streams merged to one Dart Stream you can use StreamZip from the dart:async package.

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();
}

这篇关于使用rxDart合并Firestore流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆