我在Map
类中搜索一个与List
类中的removeWhere
方法类似的方法。我现在使用以下代码:
while (map.keys.any((key) => map[key] == null)) {
map.remove(map.keys.firstWhere((key) => map[key] == null));
}
请参阅:https://dartpad.dartlang.org/e1a5c3c9fc475668375b
在dart中有没有更好的/更短的/更好的方法来做到这一点?
发布于 2015-07-01 00:23:04
这个答案最初来自来自dart slack通道的jerweb,但由于他不在stackoverflow上,我将把它放在这里。
一种更短、更快的方法是:
map.keys.where((key) => map[key] == null).toList().forEach(map.remove);
这条语句中的toList
可能看起来是多余的,但是如果没有toList
,你会得到一个错误,老实说,我不能完全理解这个错误。如果你这样做了,你可能想要留下一个评论,或者改进这个答案。
https://stackoverflow.com/questions/31150631
复制相似问题