【问题标题】:How to get documents in firestore, whose document IDs are stored in the list?, i.e bring only those documents....?如何在firestore中获取文档,其文档ID存储在列表中?,即只带那些文档....?
【发布时间】:2020-08-18 10:41:36
【问题描述】:

我有一个包含特定集合“用户”的文档 ID 的列表,我想获取仅存在于该列表中的文档,并且这些文档位于“用户”集合中,如前所述。

代码:

  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 结构: Click here for the firebase structure

图片格式的代码:

Click here for the image

  1. 在橙色框中,“users1”是一个包含文档 ID 的列表...用户 10 公里用户
  2. 在粉红色框中,我们使用列表的值...
  3. 在绿色框中,我们需要传递上述列表中存在的文档 id...

Ps:我使用“val.first.id”只是为了将文档首先出现在列表中........但是我需要带上该列表中的每个文档......所以这就是我的观点……

【问题讨论】:

  • 你能得到第一个id的文档吗?
  • @Adnankarim 我能够获得第一个 ID 的文档,我刚刚上传了 firebase 结构
  • 查看我的回答,希望对您有所帮助。

标签: firebase flutter firebase-realtime-database google-cloud-firestore google-cloud-functions


【解决方案1】:

您可以为此目的使用交易。 Firestore transactions

【讨论】:

  • 事务是用于读取然后写入文档,而不仅仅是读取。
  • 因为我是 Flutter 的新手
【解决方案2】:

这里如何从列表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。

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

【讨论】:

  • 错误:“未处理的异常:无效的参数:'certainrangeusers'的实例”,得到这个错误
  • 确保“val”是一个列表
  • 好吧,这只需要“id”而不是“某些范围用户”的列表
  • class certainrangeusers { final String id;某些范围用户({this.id}); }
  • 是列表的类 = [];
猜你喜欢
  • 1970-01-01
  • 2020-12-04
  • 2019-08-02
  • 2020-12-05
  • 2021-07-20
  • 2018-09-03
  • 2020-07-18
  • 1970-01-01
  • 2020-03-09
相关资源
最近更新 更多