Flutter 将两个 Firestore 流合并为一个流

问题描述 投票:0回答:7

我只是想执行“OR”运算并将两个查询的结果放入一个流中。

这是我的带有单个流的代码

 StreamBuilder(
    stream: Firestore.instance
        .collection('list')
        .where('id', isEqualTo: 'false')
        .orderBy('timestamp')
        .snapshots(),
    builder: (context, snapshot) {
      if (!snapshot.hasData)
        return Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Center(
              child: CircularProgressIndicator(),
            )
          ],
        );
      if (snapshot.data.documents.length == 0)
        return const Center(
          child: Text(
            "Not Available",
            style: TextStyle(fontSize: 30.0, color: Colors.grey),
          ),
        );
      return ListView.builder(
        padding: EdgeInsets.all(5.0),
        key: Key(randomString(20)),
        itemCount: snapshot.data.documents.length,
        itemBuilder: (BuildContext context, int index) {
          return ListCard(snapshot.data.documents[index]);
        },
      );
    }),

现在我想将两个流提供给同一个流构建器,而不是单个流。

我尝试了 StreamGroup,但自从 Widgets 重建后它就不起作用了

StreamGroup.merge([streamOne, streamTwo]).asBroadcastStream();

我也尝试了以下方法

 Stream<List<DocumentSnapshot>> searchResult()  {
List<Stream<List<DocumentSnapshot>>> streamList = [];

Firestore.instance
    .collection('room-list')
    .where('id', isEqualTo: 'false')
    .snapshots()
    .forEach((snap) {
  streamList.add(Observable.just(snap.documents));
});

Firestore.instance
    .collection('room-list')
    .where('id', isEqualTo: 'pending')
    .snapshots()
    .forEach((snap) {
  streamList.add(Observable.just(snap.documents));
});

var x = Observable.merge(streamList)
    .scan<List<DocumentSnapshot>>((acc, curr, i) {
  return acc ?? <DocumentSnapshot>[]
    ..addAll(curr);
});
return x;
}

这里我收到错误,应该至少有一个要合并的流。这是因为 Observable.merge(streamList) 在将项目添加到streamList之前被调用。

我只是想将两个查询的结果放入一个流中。

firebase merge flutter google-cloud-firestore rxdart
7个回答
7
投票

这应该有效。

//Change your streams here
    Stream<List<QuerySnapshot>> getData() {
        Stream stream1 = Firestore.instance.collection('list').where('id', isEqualTo: 'false').orderBy('timestamp').snapshots();
        Stream stream2 = Firestore.instance.collection('list').where('id', isEqualTo: 'true').orderBy('timestamp').snapshots();
        return StreamZip([stream1, stream2]);
      }


  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: StreamBuilder(
          stream: getData(),
          builder: (BuildContext context, AsyncSnapshot<List<QuerySnapshot>> snapshot1) {

            List<QuerySnapshot> querySnapshotData =  snapshot1.data.toList();

            //copy document snapshots from second stream to first so querySnapshotData[0].documents will have all documents from both query snapshots
            querySnapshotData[0].documents.addAll(querySnapshotData[1].documents);

            if (querySnapshotData[0].documents.isEmpty)
              return Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Center(
                    child: CircularProgressIndicator(),
                  )
                ],
              );
            if (querySnapshotData[0].documents.length == 0)
              return const Center(
                child: Text(
                  "Not Available",
                  style: TextStyle(fontSize: 30.0, color: Colors.grey),
                ),
              );

            return new ListView(
                children: querySnapshotData[0].documents.map((DocumentSnapshot document){
                 // put your logic here. You will have access to document from both streams as "document" here
                  return new ListCard(document);
                }).toList()
            );
          }
      ),
    );
  }

希望这有帮助!!!


6
投票

我还尝试合并来自 firestore 的两个流(因为查询不支持 OR)并像这样进行:

import 'package:rxdart/rxdart.dart';


Rx.combineLatest2(
   StreamQuerySnapshot1, //a snapshot from firestore
   StreamQuerySnapshot2, //another snapshot from firestore
   (var stream1, var stream2) {
    return [...stream1.docs, ...stream2.docs]; //Concatenated list
   }
)

无论哪个流发生变化,这都会发出更改,与我发现的其他解决方案相反,只有当两个流都发生更改时,该支持才会发出。


3
投票

我使用 RxDart 包来组合两个流,如下所示

RxDart - 组合最新流

final Stream<DocumentSnapshot> user = Firestore.instance
        .collection("users")
        .document(firebaseUser.uid)
        .snapshots();

    final Stream<QuerySnapshot> cards =
        Firestore.instance.collection("cards").snapshots();

    CombineLatestStream.list([user, cards]).listen((data) {
      add(LoadedHomeEvent(
        data.elementAt(0),
        data.elementAt(1),
      ));
    });

2
投票

我不确定你为什么使用 forEach 和 Observable.just()。

您可以直接合并两个 firestore 流,例如:

Observable.merge([stream1,stream2]).pipe(combineStream);

stream1/2 只是您的 Firestore 快照。


2
投票

好吧,我迟到了,但还是要把它放在那里。

您可以在查询中添加 whereIn 子句,如下所示:

Firestore.instance.collection("collection_name").where("field",whereIn:["false","true"]).snapshots();

0
投票

我发现的最好方法是使用

RxDart
中的 MergeStream

Stream<QuerySnapshot> searchResult() {
  final falseRoomStream = FirebaseFirestore.instance
      .collection('room-list')
      .where('id', isEqualTo: 'false')
      .snapshots();

  final pendingRoomStream = FirebaseFirestore.instance
      .collection('room-list')
      .where('id', isEqualTo: 'pending')
      .snapshots();

  return MergeStream([falseRoomStream, pendingRoomStream]);
}


0
投票

这是我在没有任何包的情况下解决这个问题的方法。在我的特殊情况下,同一个文档有可能会被重复,为了避免重复,我使用一组作为中介。

static Stream<List<CleaningEvent>> streamForCleaner(
    String cleanerID,
    DateTime rawDate,
  ) {
    final date = DateTime.utc(rawDate.year, rawDate.month, rawDate.day);
    StreamController<List<CleaningEvent>> controller = StreamController();
    Iterable<CleaningEvent> eventsS1 = [], eventsS2 = [];
    Set<CleaningEvent> joinedSet = {};

    update() {
      joinedSet = {};
      joinedSet.addAll(eventsS1);
      joinedSet.addAll(eventsS2);
      controller.add(joinedSet.toList());
    }

    Stream<QuerySnapshot> stream1 = _collection
        .where("cleanersIDs", arrayContains: cleanerID)
        .where("reqDate", isEqualTo: date)
        .snapshots();
    Stream<QuerySnapshot> stream2 = _collection
        .where("cleanersIDs", arrayContains: cleanerID)
        .where("schDate", isEqualTo: date)
        .snapshots();
    stream1.forEach((query) {
      eventsS1 = query.docs.map(CleaningEvent.fromDoc);
      update();
    });
    stream2.forEach((query) {
      eventsS2 = query.docs.map(CleaningEvent.fromDoc);
      update();
    });

    return controller.stream;
  }
© www.soinside.com 2019 - 2024. All rights reserved.