在Dart(Flutter)中,我能否一次性获得列表中满足条件的第一项?
目前,我获得满足条件的列表中第一项的方式如下-即1)做一个“ where” 2)然后要求“ first”:
List<Currency> currencies = ...;
Currency dollar = currencies.where((currency) => currency.code == "USD").first;
它不接受以下内容:
Currency dollar = currencies.first((currency) => currency.code == "USD");
我想念什么吗?
答案 0 :(得分:0)
您尝试过.firstWhere()
吗?
List<Currency> currencies = ...;
Currency dollar = currencies.firstWhere((currency) => currency.code == "USD");