Home > Software design >  Dart, what is the "where" keyword
Dart, what is the "where" keyword

Time:10-03

I've seen the occasional list.where() code on stackoverflow.

So I'm trying to figure this out.

How is it different from .map()?

Where can I read the documentation to learn in detail about this? (firstWhere, startWith ... etc very complex queries with => keywords)

CodePudding user response:

Here are the methods workings in example:

final list = [1, 2, 3, 4];

// List.where is used to filter out element based on a condition
list.where((element) => element.isOdd); // [1, 3]

// List.map allows you to map each value to a new one (not necessarily of the same type)
list.map((element) => 2*element); // [2, 4, 6, 8]

Where to read this information

One of the best thing of dart (and flutter) is its source code documentation. In most code editors, you can ctrl click on a method to see the documentation. From there you can learn what it does and there are sometimes example.

Another great place is StackOverflow of course, since those question are asked over and over, especially basic ones like this which are often the same across different languages.

CodePudding user response:

you can read it from here

the difference between where and map is basically this, where return a new list "iterable" with only the elements that satisfies the passed test, and map return also a new list (but with the same length), each element is mapped with the passed function to different value (wether the same type or a completely different type)

  • Related