如何从Map<String, List<dynamic>> Flutter中进行搜索?

huangapple go评论52阅读模式
英文:

How can i searching from Map<String, List<dynamic>> Flutter

问题

我正在尝试创建搜索栏并从Map<String, List<dynamic>>中进行搜索,但我遇到了以下错误 -> "类型为'Iterable<MapEntry<String, List<dynamic>>>'的值无法赋给类型为'Map<String, List<dynamic>>'的变量"

//这是我尝试的函数。
Map<String, List<dynamic>> datasource_map = {};

Map<String, List<dynamic>> result = {};
void updateList(String enteredKeyword) {

setState(() {
result = datasource_map.entries.map((e) {
return MapEntry(
e.key,
e.value
.where(
(element) => element.foodName!.toLowerCase().contains(
enteredKeyword.toLowerCase(),
),
)
.toList());
});
print("### $result");
});
}

这是我的foodCard模型

我应该尝试什么?我是Flutter的新手。

英文:

i'm try to make Search Bar and Searching from Map<String, List<dynamic>>
but i got error like this --> "A value of type 'Iterable<MapEntry<String, List<dynamic>>>'
can't be assigned to a variable of type 'Map<String, List<dynamic>>"

//here the function that i try.
Map<String, List<dynamic>> datasource_map = {};

Map<String, List<dynamic>> result = {};
void updateList(String enteredKeyword) {

setState(() {
  result = datasource_map.entries.map((e) {
    return MapEntry(
        e.key,
        e.value
            .where(
              (element) =&gt; element.foodName!.toLowerCase().contains(
                    enteredKeyword.toLowerCase(),
                  ),
            )
            .toList());
  });
  print(&quot;### $result&quot;);
});

}

this is my Model of foodCard

what should i try ? i am new at flutter

答案1

得分: 0

看起来你的 datasource_map.entries 是一个列表 - 对它调用 map() 方法会产生一个迭代器 - 而不是一个 Map 对象。

你应该使用 Map.fromEntries 构造函数:

result = Map.fromEntries(datasource_map.entries.map((e) {
    return MapEntry(
        e.key,
        e.value
            .where(
                (element) => element.foodName!.toLowerCase().contains(
                    enteredKeyword.toLowerCase(),
                ),
            )
            .toList());
}));
英文:

Looks like your datasource_map.entries is a List - calling a map() on it will produce an Iterator - not a Map object.

You should use Map.fromEntries constructor:


result = Map.fromEntries(datasource_map.entries.map((e) {
    return MapEntry(
        e.key,
        e.value
            .where(
              (element) =&gt; element.foodName!.toLowerCase().contains(
                    enteredKeyword.toLowerCase(),
                  ),
            )
            .toList());
  }));


答案2

得分: 0

谢谢大家!我解决了。

这个代码段如下所示:

Map<String, List<dynamic>> result = {};

void updateList(String enteredKeyword) {
  Map<String, List<dynamic>> mapResult = {};

  datasource_map.forEach((key, value) {
    List foodByFilter = value.where((element) {
      return element.foodName
          .toLowerCase()
          .contains(_textEditingController.text.toLowerCase().toString());
    }).toList();

    if (foodByFilter.isNotEmpty) mapResult[key] = foodByFilter;
  });

  setState(() {
    result = mapResult;
  });
}
英文:

Thank you everyone! I solved it.

This works:

Map&lt;String, List&lt;dynamic&gt;&gt; result = {};

  void updateList(String enteredKeyword) {
    Map&lt;String, List&lt;dynamic&gt;&gt; mapResult = {};

    datasource_map.forEach((key, value) {
      List foodByFilter = value.where((element) {
        return element.foodName
            .toLowerCase()
            .contains(_textEditingController.text.toLowerCase().toString());
      }).toList();

      if (foodByFilter.isNotEmpty) mapResult[key] = foodByFilter;
    });

    setState(() {
      result = mapResult;
    });
  }

huangapple
  • 本文由 发表于 2023年2月8日 13:49:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75381797.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定