Google Dart: How does the .where () function work?

var fruits = ['apples', 'oranges', 'bananas'];
fruits[0]; // apples
fruits.add('pears');
fruits.length == 4;
fruits.where((f) => f.startsWith('a')).toList();

An example in the documentation shows above. I don’t even understand the documentation for this method.

https://api.dartlang.org/stable/1.21.1/dart-collection/IterableMixin/where.html

Currently, I see the lambda function as a parameter inside, where with the argument f. What is f? I'm a little confused.

It would be great if I could see a working example. As it stands now, I really don't understand. I don’t know how it works or what it really does, except that it acts like some kind of filter.

+4
source share
1 answer

It is an anonymous function and fis the parameter that it takes

(f) => f.startsWith('a')

where(...) fruits , , true

where(...) , , , , .toList().

DartPad

"" , ,

myFilter(f) => f.startsWith('a');

main() {
  fruits.where(myFilter).toList();
}

myFilter(f) => f.startsWith('a');

myFilter(f) {
  return f.startsWith('a');
}
+4

Source: https://habr.com/ru/post/1667958/


All Articles