【问题标题】:Using where method with List<Map> Dart使用 List<Map> Dart 的 where 方法
【发布时间】:2019-05-15 04:50:45
【问题描述】:

我在颤动时遇到问题我想做一个搜索栏,我有一个对 api 的响应,我想显示来自服务器的数据,其中包含来自搜索栏的文本我的问题是方法不适用于 List有什么想法吗?

【问题讨论】:

  • 您遇到什么错误?你能告诉我们你使用的代码吗?
  • var map = [{ "city": "Amsterdam", 'address':"Flangomn", "lat":"27.012", 'lng':"43.12" }]; if (this._searchQuery.text == query &amp;&amp; this.mounted) { setState(() { _isSearching = false; if (map != null) { for(var i = 0; i &lt;map.length; i++) _results = jsonDecode(map[i]['address']).where((p)=&gt;p.startsWith(query)).toList(); } else { _error = 'Error searching'; } }); }

标签: list api dictionary dart flutter


【解决方案1】:

查看您的评论,String 类型上没有 where 方法。除了for 循环,您还需要以下内容:

// Remove the for loop
// for(var i = 0; i <map.length; i++) { _results = jsonDecode(map[i]['address']).where((p)=>p.startsWith(query)).toList(); }

// Do this instead
_results = map.where((item) => item['address'].startsWith(query)).toList();

您现在应该可以放弃 for 循环了。

【讨论】:

  • 你的回答是真的,我昨天在这篇帖子后 2 小时发现了它
猜你喜欢
  • 2021-03-25
  • 2019-11-24
  • 2020-04-08
  • 2021-05-14
  • 2021-03-27
  • 1970-01-01
  • 2016-08-22
  • 2017-11-02
  • 1970-01-01
相关资源
最近更新 更多