类Map是否有类似于List类的removeWhere方法的方法?

时间:2015-07-01 00:17:40

标签: dart

我正在Map类中搜索与removeWhere类中的List方法类似的方法。我现在使用这个代码:

while (map.keys.any((key) => map[key] == null)) {
  map.remove(map.keys.firstWhere((key) => map[key] == null));
}

请参阅:https://dartpad.dartlang.org/e1a5c3c9fc475668375b

在飞镖中有更好/更短/更好的方法吗?

1 个答案:

答案 0 :(得分:2)

这个答案最初来自dart slack频道的jerweb,但由于他不在stackoverflow上,我会把它放在这里。

更短更快的方法是:

map.keys.where((key) => map[key] == null).toList().forEach(map.remove);

此声明中的toList可能看起来多余,但没有toList您会收到错误,说实话,我完全不了解此错误。如果您这样做,您可能希望发表评论或改进此答案。

这是workin演示: https://dartpad.dartlang.org/a3b98fca58162169781a