dart - Adding orElse function to firstWhere method -
dart - Adding orElse function to firstWhere method -
i trying add together onelse function itterator.firstwhere method cannot syntax right.
i have tried like
class="lang-dart prettyprint-override">list<string> mylist = string result = mylist.firstwhere((o) => o.startswith('foo'), (o) => null);
but compiler has error of
1 positional arguments expected, 2 found
i sure simple syntax problem, has me stumped
'orelse' named optional argument.
class="lang-dart prettyprint-override">void main() { checkorelse(['bar', 'bla']); checkorelse(['bar', 'bla', 'foo']); } void checkorelse(list<string> values) { string result = values.firstwhere((o) => o.startswith('foo'), orelse: () => ''); if (result != '') { print('found: $result'); } else { print('nothing found'); } }
dart
Comments
Post a Comment