Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从两个集合获取数据-Firebase/Flatter_Firebase_Flutter_Google Cloud Firestore - Fatal编程技术网

从两个集合获取数据-Firebase/Flatter

从两个集合获取数据-Firebase/Flatter,firebase,flutter,google-cloud-firestore,Firebase,Flutter,Google Cloud Firestore,我有一个关于我的应用程序的问题。我有两个集合,称为X和Y。我需要从这些集合的特定字段中获取数据。“X”和“Y”集合具有与“名称”相同的字段。我一直在尝试从X和Y集合中获取名称,并将它们列在我的页面上。我的代码: getNames(myId) async{ return await FirebaseFirestore.instance.collection("X").where('ID',isEqualTo: myId ).snapshots(); } 我可以

我有一个关于我的应用程序的问题。我有两个集合,称为X和Y。我需要从这些集合的特定字段中获取数据。“X”和“Y”集合具有与“名称”相同的字段。我一直在尝试从X和Y集合中获取名称,并将它们列在我的页面上。我的代码:

 getNames(myId) async{

    return await FirebaseFirestore.instance.collection("X").where('ID',isEqualTo: myId ).snapshots();
  }
我可以从X集合中获取名称,并使用此代码在我的页面上列出它们。但我无法同时获得Y集合名称和X集合名称,并将它们列在我的页面上。 提前感谢您的关注和帮助。

getNames(myId)async{
  getNames(myId) async{  
    QuerySnapshot response1  = await FirebaseFirestore.instance.collection("X").where('ID',isEqualTo: myId ).get();
    QuerySnapshot response2 = await FirebaseFirestore.instance.collection("Y").where('ID',isEqualTo: myId ).get();
    List<DocumentSnapshot> list1 = response1.docs;
    list1.addAll(response2.docs);
    return list1;
}
QuerySnapshot response1=wait FirebaseFirestore.instance.collection(“X”).where('ID',isEqualTo:myId).get(); QuerySnapshot response2=wait FirebaseFirestore.instance.collection(“Y”).where('ID',isEqualTo:myId).get(); List list1=响应1.docs; 列表1.addAll(response2.docs); 返回列表1; }
如果有两个集合,则需要两个查询。Firestore将不会跨多个集合进行查询,但可跨同名的所有集合进行查询的除外。这取决于你分别进行这两个查询,然后将结果合并到你的应用程序代码中。要么如此,要么将所有数据合并到一个集合中,或许使用一个区分字段来区分X和Y。@DougStevenson感谢您的回答。我明白你的意思。第二个选项是将所有数据合并到一个集合中,这将使应用程序变得如此复杂。我选择了第一个选择,我试过了,但是没有达到我想要的。比如wait FirebaseFirestore.instance.collection(“X”).where('ID',isEqualTo:myId).snapshots();return wait wait FirebaseFirestore.instance.collection(“Y”).where('ID',isEqualTo:myId).snapshots();当我这样做时,我只看到Y集合中的名称。如果你帮了我的忙,我会很高兴。这听起来像是一个不同于你现在遇到的问题。如果你坚持把所有的东西都放在一个集合中,请发布一个新问题。我不想把它们放在一个集合中。我只是想在我的页面上发布不同集合中的相同字段。如果您进行网络搜索,我相信您可以在dart中找到有关合并两个流的信息。谢谢您的回答。Stream1=await FirebaseFirestore.instance.collection(“X”).where('ID',isEqualTo:myId).snapshots();stream2=await FirebaseFirestore.instance.collection(“Y”).where('ID',isEqualTo:myId).snapshots();这里如何合并两条流?返回。。。。;我真的很抱歉我给了你一个错误的代码,我刚刚编辑了我的答案。现在应该可以了。如果你有任何问题,请告诉我。再次抱歉。返回StreamZip([stream1,stream2])。asBroadcastStream();StreamZip不适用于itI很抱歉,我不熟悉此StreamZip,但这可能会有所帮助。addAll可能会给出一个错误,该错误表示“此表达式的类型为void,因此其值无法使用”