0

我有一个动态填充的字符串列表(最多 5 个项目)。现在我想根据这些项目从 firsestore 过滤我的文档,所以我写了这个:

List<String> lst= new List();
lst.add('A');
lst.add('B');
lst.add('C');
    Query collectionRef = Firestore.instance
        .collection("files")
        .document(Userpref.language)
        .collection("files")
        .where("describeFear", arrayContainsAny: [
       lst
    ]);

如果我在 [] 中输入列表名称,它将不起作用。此外,如果我将列表中的所有项目放在一个变量中并将其传递给 [] 它将不起作用。

它唯一有效的时候是当我输入像这样的值'A','B','C'。这可行,但我需要将数组传递给它的动态。任何人都可以帮忙吗?

4

3 回答 3

2

最新版本:Flutter + cloud_firestore:^0.14.0+2

我面临着类似的问题,查询返回空数据:

          FutureBuilder(
          future: _sessionsLogCollection
              .where('companyId', isEqualTo: sPData.companyId)
              .where('locationId', arrayContainsAny: [
                '29L73oSzdQrzLUow3Mg9',
                'bugdWVC6RRtHoemuxNWE',
              ])
              // .where('locationId', isEqualTo: 'bugdWVC6RRtHoemuxNWE')
              .orderBy('openDateTime', descending: true)
              .get(),

我已经创建了索引,所以这不是问题。

单独使用.where('locationId', isEqualTo: 'bugdWVC6RRtHoemuxNWE')时,查询返回正确的数据。但是单独使用.where('locationId', arrayContainsAny: ['29L73oSzdQrzLUow3Mg9','bugdWVC6RRtHoemuxNWE'])不会给出任何错误,而只是返回空数据集:sessionsSnapShot.data.documents.length: 0

编辑:当 locationId 本身是一个数组时, arrayContainsAny: 将起作用。然而,如果 locationId 是一个字符串字段,那么应该使用whereIn:。

于 2020-09-04T09:54:31.247 回答
1

对于此用例,您需要使用 whereIn 而不是 arrayContainsAny

List<String> lst= new List();
lst.add('A');
lst.add('B');
lst.add('C');

Query collectionRef = Firestore.instance
    .collection("files")
    .document(Userpref.language)
    .collection("files")
    .where("describeFear", whereIn: [
   lst
]);

于 2021-03-20T13:52:17.053 回答
0

您将List对象放入一个数组中,因此您最终会寻找具有一个数组的文档,其中一个项目是["A", "B", "C"].

你想要的是:

List<String> lst= new List();
lst.add('A');
lst.add('B');
lst.add('C');

Query collectionRef = Firestore.instance
    .collection("files")
    .document(Userpref.language)
    .collection("files")
    .where("describeFear", arrayContainsAny: lst);
于 2020-07-09T14:09:26.863 回答