我正在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
在飞镖中有更好/更短/更好的方法吗?
答案 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