如何比较两个不同的Firebase集合的两个不同的流,并返回Flutter中两个文档中都存在的文档? [英] How do I compare two different streams of two different collections of firebase and return a document which is present in both in Flutter?

查看:76
本文介绍了如何比较两个不同的Firebase集合的两个不同的流,并返回Flutter中两个文档中都存在的文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

`getMatches(String userId)异步{

 返回StreamBuilder(流:Firestore.instance.collection(用户").document(userId).collection("chosenList").snapshots(),生成器:(上下文,快照){返回StreamBuilder(流:Firestore.instance.collection(用户").document(userId).collection("selectedList").snapshots(),生成器:(上下文,快照1){if(!snapshot.hasData ||!snapshot1.hasData){返回CircularProgressIndicator();}对于(snapshot.data中的DocumentSnapshot doc){for(snapshot1.data中的DocumentSnapshot文档){if(doc.documentID == docs.documentID){打印(docs.documentID);}}}});}); 

}`我有两个不同的集合,其中包含带有userId的文档列表.我需要使用流比较它们两个并返回两个中都存在的文档.我该怎么办?

解决方案

您可以使用 rx包具有Dart Streams的更多功能,那么您可以使用 CombineLatestStream.combine2< R,T,S>(Stream< r>,Stream< T> t,S组合器(A a,B b)

  import'package:rxdart/rxdart.dart';流< DocumentSnapshot>合并=>CombineLatestStream.combine2< FirebaseUser,FirebaseUser,DocumentSnapshot>(MyFirebaseStream.document(userId).snapshots(),//或第一个流是MyOtherFirebaseStream.document(otherUserId).snapshots(),(documentFirstStream,documentSecondStream)=>{//执行逻辑以在此处提取并返回满足您条件的文档}); 

如果要成对比较(每个流的第一个元素,每个流的第二个元素,等等),还有其他方法可以尝试,具体取决于您想要的是 ZipStream.zip2 ./p>

也许有一些代码可以知道您尝试从每个流中确切返回什么内容,从而可以帮助您更多

`getMatches(String userId) async {

return StreamBuilder(
  stream: Firestore.instance.collection("users")
  .document(userId).collection("chosenList").snapshots(),
  builder: (context, snapshot) {
    return StreamBuilder(
      stream: Firestore.instance.collection("users")
  .document(userId).collection("selectedList").snapshots(),
      builder: (context, snapshot1) {
        if(!snapshot.hasData || !snapshot1.hasData) {
          return CircularProgressIndicator();

        }
        for (DocumentSnapshot doc in snapshot.data) {
          for(DocumentSnapshot docs in snapshot1.data) {
            if(doc.documentID == docs.documentID) {
              print(docs.documentID);
            }
          }
        }

      });

  }
  
  );

}`I have a two different collections which has a list of documents with userId's. I need to compare both of them using stream and return the document which is present in both. How can I do that?

解决方案

You can use the rx package to have more capabilities to Dart Streams, then you can use CombineLatestStream.combine2<R, T, S>(Stream<R> r, Stream<T> t, S combiner(A a, B b) like this

import 'package:rxdart/rxdart.dart';

Stream<DocumentSnapshot> get combined => CombineLatestStream.combine2<FirebaseUser, FirebaseUser, DocumentSnapshot>(
     MyFirebaseStream.document(userId).snapshots(), //or whatever your first stream is
     MyOtherFirebaseStream.document(otherUserId).snapshots(), 
     (documentFirstStream, documentSecondStream) => {
        // Do the logic to extract and return the document that satisfy your condition here 
     }
   );

There are other methods to try depending of what you want exactly, ZipStream.zip2 if you want to compare by pair (first element of each stream, second element of each stream, etc).

Maybe a bit of code to know what exactly you try to return from each stream could help to guide you more

这篇关于如何比较两个不同的Firebase集合的两个不同的流,并返回Flutter中两个文档中都存在的文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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