where Method in Dart,flutter

yousaf@345
3 min readMay 6, 2024

--

The `where` method in Dart is a powerful tool for filtering elements in collections like lists. It takes a predicate function as an argument and returns a new iterable containing only the elements that satisfy the condition. Here are some examples that demonstrate different ways to use the `where` method:

  1. Filtering Even Numbers
void main() {
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var evenNumbers = numbers.where((number) => number % 2 == 0);
print(evenNumbers.toList()); // Output: [2, 4, 6, 8, 10]
}

Explanation of above example the code works:

1. List Creation: The code creates a list called `numbers` containing the numbers from 1 to 10.

2. Filtering Even Numbers: The `where` method is called on the `numbers` list. This method takes a callback function (also known as a predicate) as an argument. The callback function `(number) => number % 2 == 0` checks if a number is even by using the modulo operator `%` to check if the number divided by 2 has a remainder of 0. If the condition is true, the number is included in the result.

3. Storing Filtered Numbers: The result of the `where` method is an iterable (a type of collection) containing the even numbers from the `numbers` list. This iterable is stored in the `evenNumbers` variable.

4. Printing the Result: The `evenNumbers` iterable is converted to a list using the `toList()` method, and the resulting list is printed using the `print` function. The output is `[2, 4, 6, 8, 10]`, which are the even numbers from 1 to 10.

2. Filtering Strings Longer Than 3 Characters

void main() {
var words = ['apple', 'banana', 'pear', 'kiwi', 'orange'];
var longWords = words.where((word) => word.length > 3);
print(longWords.toList()); // Output: ['apple', 'banana', 'orange']
}

This above example code defines a list of words, filters out the words with more than 3 characters using the where method, and then prints the result. The where method takes a predicate function that returns true for elements that should be included in the output.

3. Filtering Numbers Greater Than 5

void main() {
var numbers = [1, 5, 10, 15, 20];
var greaterThan5 = numbers.where((number) => number > 5);
print(greaterThan5.toList()); // Output: [10, 15, 20]
}

4. Filtering Even-Indexed Elements

void main() {
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var evenIndexedNumbers = numbers.where((number) => numbers.indexOf(number) % 2 == 0);
print(evenIndexedNumbers.toList()); // Output: [1, 3, 5, 7, 9]
}

This above example code defines a list of numbers from 1 to 10, filters out the numbers at even indices using the where method along with indexOf, and then prints the result. The where method takes a predicate function that returns true for elements that should be included in the output.

5. Filtering Elements Based on Type

void main() {
var mixedList = [1, 'two', 3, 'four', 5];
var numbersOnly = mixedList.where((element) => element is int);
print(numbersOnly.toList()); // Output: [1, 3, 5]


}

6. Filtering Elements in a Range

void main() {
var numbers = [10, 20, 30, 40, 50];
var numbersInRange = numbers.where((number) => number >= 20 && number <= 40);
print(numbersInRange.toList()); // Output: [20, 30, 40]


}

7. Filtering Null Values

void main() {
var values = [1, null, 3, null, 5];
var nonNullValues = values.where((value) => value != null);
print(nonNullValues.toList()); // Output: [1, 3, 5]


}

These examples showcase the versatility of the `where` method in Dart, allowing you to filter elements based on various conditions easily. Whether you’re working with numbers, strings, or complex data structures, the `where` method can help you extract the elements you need efficiently.

--

--