如何在Firestore中获取文档ID已存储在列表中的文档?即只带那些文档....? [英] How to get documents in firestore, whose document IDs are stored in the list?, i.e bring only those documents....?

本文介绍了如何在Firestore中获取文档ID已存储在列表中的文档?即只带那些文档....?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表,其中包含特定集合用户"的文档ID,并且我希望仅在此列表中显示文档,并且这些文档如前所述位于用户"集合中.

I have a list that contains the document Ids of a particular collection "users", and I want to get documents only present in this list, and these documents are in "users" collection as mention before.

代码:

  getUsers() async
     {
       double radius = 0.3;
      String field = "GeoPosition";
       print(currentUser.point.longitude);
       GeoFirePoint center = geo.point(latitude: currentUser.point.latitude,  longitude: currentUser.point.longitude);
        var collectionRef = Firestore.instance.collection('user_locations').document(currentUser.pincode)  .collection('Users_complete_address_and_geopoint');
        this.stream =  geo.collection(collectionRef: collectionRef).within(center: center, radius: radius, field: field, strictMode: false);   
        Future<List<certainrangeusers>> users1 =  stream.first.then(
        (documents) => documents.map((doc) => certainrangeusers(
         id: doc['userPhone'],
         )
        ).toList());  
         users1.then((val) async{
        QuerySnapshot snapshot = await  Firestore.instance.collection('users').where('id', isEqualTo: val.first.id).getDocuments();
         List<User> users = snapshot.documents.map((doc) =>  User.fromDocument(doc)).toList();
        setState(() {
         this.users = users;
          });
       });
   }

Firebase结构: 单击此处获取Firebase结构

Firebase Structure: Click here for the firebase structure

代码以图像格式:

点击此处查看图片

  1. 在橙色框中,"users1"是一个列表,其中包含10公里用户的文档ID ...
  2. 在粉红色框中,我们使用列表的值...
  3. 在绿色框中,我们需要传递上面列表中显示的文档ID ...

Ps:我使用"val.first.id"只是为了将文档首先出现在列表中.....但是我需要将列表中的每个文档都显示在其中...所以我触及到的一点...

P.s: I am using "val.first.id" just to bring the document present at first in the list........But I need to bring every document present in that list...so that's a point I have struck...

推荐答案

这里是如何从列表ID中搜索所有文档

Here how to search for all documents from the list id

代替:

await  Firestore.instance.collection('users').where('id', isEqualTo: 
 val.first.id).getDocuments();

使用此:

await  Firestore.instance.collection('users').where('id', whereIn: 
 val).getDocuments();

由于您有一个certainchangeuser列表,请执行此操作以获取ID.

Since you have a list of certainchangeuser, do this to get the ids.

users1.then((val){
  // Get the ids
  List ids = [];
  for (var value in val){
    ids.add(value.id);
  }
  //Then
  QuerySnapshot = await 
    Firestore.instance.collection('users').where('id', 
    whereIn:ids).getDocuments();
....
});

这篇关于如何在Firestore中获取文档ID已存储在列表中的文档?即只带那些文档....?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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