Where in Dart

Where Dart

You can use where in list, set, map to filter specific items. It returns a new list containing all the elements that satisfy the condition. This is also called Where Filter in dart. Let’s see the syntax below:

Syntax

Iterable<E> where(
bool test(
E element
)
)

Example 1: Filter Only Even Number From List

In this example, you will get only odd numbers from a list.

void main() {
  List<int> numbers = [2, 4, 6, 8, 10, 11, 12, 13, 14];

  List<int> oddNumbers = numbers.where((number) => number.isOdd).toList();
  print(oddNumbers);
}

Show Output
Run Online

Example 2: Filter Days Start With S

In this example, you will get only days that start with alphabet s.

void main() {
  List<String> days = [
    "Sunday",
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday"
  ];

  List<String> startWithS =
      days.where((element) => element.startsWith("S")).toList();

  print(startWithS);
}

Show Output
Run Online

Example 3: Where Filter In Map

In this example, you will get students whose marks are greater or equal to 32.

void main() {
  Map<String, double> mathMarks = {
    "ram": 30,
    "mark": 32,
    "harry": 88,
    "raj": 69,
    "john": 15,
  };

  mathMarks.removeWhere((key, value) => value < 32);

  print(mathMarks);
}

Show Output
Run Online

Video

Watch our video on the where in Dart.