我创建了一个过滤器,该过滤器可基于属性匹配对象,并且在基于现有json数据时可以运行,但是,我现在尝试基于现有列表进行过滤,并且无法使新的过滤器正常工作。这里的对象是获取所有匹配的属性,并将它们放入“标题项目”列表中,然后将匹配的实际产品放入“ ProductsInHeading”中。
问题是我无法获得推荐的产品列表来匹配原始列表中列出的任何产品。这是两个有效的示例,后面是无效的示例。
工作:
switch (selectedTab) {
case FilterTabs.Feels:
final Map<String, List<Product>> allFeelings =
Map.fromEntries(TabCategories.feeling.map((e) => MapEntry(e, [])));
Map<String, List<Product>> headingItems =
items.fold(allFeelings, (feelings, element) {
if (!feelings.containsKey(element.effect)) {
return feelings;
}
return feelings
..update(element.effect, (value) => value..add(element));
});
productList = headingItems;
print("headingItems: $headingItems");
return headingItems.entries
.map((e) => ProductsInHeading(e.key, e.value..sort()))
.toList()
..sort()
..where((e) => e.products.length != 0);
break;
case FilterTabs.Categories:
// usage might be wrong, I'm not sure
final Map<String, List<Product>> allCategories = Map.fromEntries(
TabCategories.prodCategory.map((e) => MapEntry(e, [])));
Map<String, List<Product>> headingItems =
items.fold(allCategories, (categories, element) {
if (!categories.containsKey(element.productCategory)) {
return categories;
}
return categories
..update(element.productCategory, (value) => value..add(element));
});
// print("headingItems: $headingItems");
productList = headingItems;
return headingItems.entries
.map((e) => ProductsInHeading(e.key, e.value..sort()))
.toList()
..sort()
..where((e) => e.products.length != 0);
break;
但是,过滤器的这一部分不起作用。推荐产品包含4个元素:
Happy, Hungry, Sleepy, Thirsty
推荐类别列表包含这些属性
Happy, Hungry, Sleepy, Thirsty, Awake, Alert, Relaxed, and Mellow.
但是,当我运行该函数时,没有任何匹配。这就是我现在所拥有的:
case FilterTabs.Rec:
final Map<String, List<Product>> allRecommended =
Map.fromEntries(recCategories.map((e) => MapEntry(e, [])));
Map<String, List> headingItems =
items.fold(allRecommended, (rec, element) {
if (!rec.containsValue(element.effect) ||
!rec.containsValue(element.usage)) {
return rec;
}
return rec
..update(recommendedProducts.toString(), (value) => value..add(element));
});
productList = headingItems;
print("headingItems: $headingItems");
return headingItems.entries
.map((e) => ProductsInHeading(e.key, e.value..sort()))
.toList()
..sort()
..where((e) => e.products.length != 0);
break;