gpt4 book ai didi

flutter - 消防站 : Query Multiple Search Term With Map Not working as Expected

转载 作者:IT王子 更新时间:2023-10-29 07:15:03 28 4
gpt4 key购买 nike

场景

我有一个类(class)集合,我想根据用户输入的逗号分隔词来搜索集合。但是,在执行查询时会返回所有类(class)集合。

Flutter 中的代码片段

    Future<void> search(List<String> keywords) async {
var docs = coursesRef;
// BELOW I ITERATE OVER KEYWORDS CONVERT
// THEM TO LOWER CASE AND APPEND TERM TO MAP searchTerms
keywords.forEach((word) {
String term = word.trim().toLowerCase();
docs.where('searchTerms.$term', isEqualTo: true);
});
docs.where('isDraft', isEqualTo: false);

QuerySnapshot query = await docs.getDocuments();

courses = [];
query.documents.forEach((doc) {
Course course = Course.fromDoc(doc);
courses.add(course);
});

notifyListeners();
}

我的 map 字段如下所示:

enter image description here

返回的搜索结果

下面是否显示当前所有类(class)集合:

    {uid: 3riP6kUweNPF2EV7avD2, name: Chemistry, code: CHEM208, academy: American University of Beirut, academyCode: AUB, isDraft: false, keywords: [chem208, chem 208, chemistry]}
{uid: KyyTpfEbbG8FhJbKvsuo, name: Math, code: MATH204, academy: American University of Beirut, academyCode: AUB, isDraft: false, keywords: [math204, math 204, math]}
{uid: jTBaY0T2CHPUGKPg8tb8, name: Chemistry, code: CHEM200, academy: American University of Beirut, academyCode: AUB, isDraft: false, keywords: [chem200, chem 200, chemistry]}
{uid: vcdLCmrwN4RPKDt7CeUX, name: Accounting, code: ACC 213, academy: American University of Beirut, academyCode: AUB, isDraft: false, keywords: [accounting, acc213, acc 213]}

给定

关键字只包含术语数学

最佳答案

每次调用 where(...) 时,它都会返回一个新的 Query 对象。因此,每次添加条件时,您都需要使用新查询更新变量:

var docs = coursesRef;
keywords.forEach((word) {
String term = word.trim().toLowerCase();
docs = docs.where('searchTerms.$term', isEqualTo: true);
});
docs = docs.where('isDraft', isEqualTo: false);

关于flutter - 消防站 : Query Multiple Search Term With Map Not working as Expected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57633903/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com