将 Map<key, value> 转换为 List<value>

Mar*_*in. 4 dictionary list type-conversion dart flutter

我一直在寻找一段时间没有成功。我正在尝试将地图转换为列表(丢弃键)。

当然,我可以

var addictionList = new List<AddictionDay>();
dataMan.forEach((String date, AddictionDay day) {
    addictionList.add(day);
});
Run Code Online (Sandbox Code Playgroud)

但是有没有更直接的方法,比如

var addictionList = dataMan.toList(); 
Run Code Online (Sandbox Code Playgroud)

?

mapMap 的功能似乎没有多大帮助,因为它只接受MapEntry<K2, V2>作为参数,所以这是不行的。很惊讶之前没有人问过这个问题。

我也在考虑扩展 Map 的新类,但现在我只是在研究一些本地方式来做到这一点,如果它存在的话。

Spe*_*ion 9

Map 类具有entries属性,该属性将地图条目作为可迭代对象返回,您可以在其上使用map函数生成所需的项目列表。

var addictionList = dataMan.entries.map((MapEntry mapEntry) => mapEntry.value).toList();
Run Code Online (Sandbox Code Playgroud)

mapEntry.key是的类型StringmapEntry.value是的类型AddictionDay


mez*_*oni 8

您需要这个还是更复杂的东西?

  var _map = {1: 'one', 2: 'two', 3: 'three'};
  var values =_map.values.toList();
  print(values);
Run Code Online (Sandbox Code Playgroud)

结果:

[one, two, three]