【问题标题】:How to search and display results from an array field in cloud firestore in flutter?如何在flutter中搜索和显示cloud firestore中数组字段的结果?
【发布时间】:2020-07-12 06:41:16
【问题描述】:

我的搜索功能背后的逻辑是,searchKey 字段存储了一个值的大写首字母。基本上,我将使用它的 searchKey 存储任何值;这是值首字母大写并存储在名为 SearchKey 的新字段中。

我想显示来自 Cloud Firestore 中数组的搜索结果。我为字符串字段编写的代码完美运行。这里是:

void initiateSearch(String val) async {
if (val.length == 0) {
  setState(() {
    queryResultSet = [];
    tempSearchStore = [];
    queryResultGigSet = [];
    tempSearchGigStore = [];
    queryResultTagSet = [];
    tempSearchTagStore = [];
  });
}

String capitalizedValue =
    val.substring(0, 1).toUpperCase() + val.substring(1);
    if (queryResultGigSet.length == 0 && val.length == 1) {
  // SearchService().searchByName(val);

  await databaseReference
      .collection('posts')
      .where('searchKey', isEqualTo: val.substring(0, 1).toUpperCase())
      .getDocuments()
      .then((QuerySnapshot docs) {
    for (int i = 0; i < docs.documents.length; ++i) {
      setState(() {
        isLoading = false;
        queryResultGigSet.add(docs.documents[i].data);
      });
    }
  });
} else {
  tempSearchGigStore = [];
  queryResultGigSet.forEach((element) {
    if (element['category'].startsWith(capitalizedValue)) {
      setState(() {
        isLoading = false;
        tempSearchGigStore.add(element);
      });
    }
  });
}

但是对于一个数组它不起作用。我写的代码是:

    if (queryResultTagSet.length == 0 && val.length == 1) {
  // SearchService().searchByName(val);

  await databaseReference
      .collection('posts')
      .where('searchKeyTags', arrayContains: val.substring(0, 1).toUpperCase())
      .getDocuments()
      .then((QuerySnapshot docs) {
    for (int i = 0; i < docs.documents.length; ++i) {
      setState(() {
        isLoading = false;
        queryResultTagSet.add(docs.documents[i].data);
      });
    }
  });
} else {
  tempSearchTagStore = [];
  queryResultTagSet.forEach((element) {
    if (element['tags'].values.startsWith(capitalizedValue)) {
      setState(() {
        isLoading = false;
        tempSearchTagStore.add(element);
      });
    }
  });
}
}
 }

【问题讨论】:

    标签: flutter google-cloud-firestore flutter-layout flutter-dependencies


    【解决方案1】:

    答案是

        if (queryResultTagSet.length == 0 && val.length == 1) {
      // SearchService().searchByName(val);
    
      await databaseReference
          .collection('posts')
          .where('searchKeyTags',
              arrayContains: val.substring(0, 1).toUpperCase())
          .getDocuments()
          .then((QuerySnapshot docs) {
        for (int i = 0; i < docs.documents.length; ++i) {
          setState(() {
            isLoading = false;
            queryResultTagSet.add(docs.documents[i].data);
          });
        }
      });
    } else {
      tempSearchTagStore = [];
      queryResultTagSet.forEach((element) {
         List.from(element['tags']).forEach((p) {
          if (p.toString().startsWith(capitalizedValue)) {
            setState(() {
              isLoading = false;
              tempSearchTagStore.add(element);
            });
          }
        });
      });
    }
    

    【讨论】:

    • 您能接受您自己的答案吗?通过这种方式,社区的其他成员可以识别出该问题具有可接受且有效的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-04
    • 2021-06-13
    • 1970-01-01
    • 1970-01-01
    • 2019-11-05
    • 2020-07-08
    相关资源
    最近更新 更多