如何在Dart中一次性获得满足条件的列表中的第一项?

时间:2018-12-25 19:08:26

标签: dart

在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");

我想念什么吗?

1 个答案:

答案 0 :(得分:0)

您尝试过.firstWhere()吗?

List<Currency> currencies = ...;
Currency dollar = currencies.firstWhere((currency) => currency.code == "USD");