【发布时间】: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