Dart| Flutter How to: Filter search a list with examples

This tutorial shows multiple examples of filtering List with some logic in Dart and flutter

Dart/flutter multiple ways to filter a list or array of objects

Following are multiple ways we can filter a list of objects

In both the examples below, we have a list of objects, Where each object contains salary and name.

Filter the list of objects based on salary conditions and output the result.

  • using Where predicate The List.where() method allows you to iterate the list and apply the predicate function and returns a stream of elements. toList() method takes an element from a stream of elements to append to a list and finally, returns the list of elements.

Here is an example code program

void main() {
  List emps = [
    {"salary": 5000, "name": "john"},
    {"salary": 6000, "name": "andre"},
    {"salary": 8000, "name": "mark"},
    {"salary": 11000, "name": "flint"}
  ];
  List result = emps.where((o) => o['salary'] > 10000).toList();

  print(result);
}

output:

[{salary: 11000, name: flint}]
  • use the for-in loop

This is for the loop to iterate the list of objects and conditions is added inside it too and return the object.

void main() {
  List emps = [
    {"salary": 5000, "name": "john"},
    {"salary": 6000, "name": "andre"},
    {"salary": 8000, "name": "mark"},
    {"salary": 11000, "name": "flint"}
  ];
  var result = [
    for (var emp in emps)
      if (emp["salary"] > 7000) emp
  ];

  print(result);
}

Output:

[{salary: 8000, name: mark}, {salary: 11000, name: flint}]

List filter based on the first letter of a string

This example filter list of strings with condition list strings that stars with one letter This example uses List.where() based on the conditional predicate.

void main() {
  var words = ['one', 'two', 'three'];
  List<String> result = words.where((f) => f.startsWith('t')).toList();
  print(result);
}

Output:

[two, three]

Dart list filter based on a number less than the value

Create a list of numbers.

You want to filters the numbers which are greater than 100. This example uses List.where() based on the conditional predicate.

void main() {
  var numbers = [11, 34, 35, 150, 20, 200];
  List<int> numberResult = numbers.where((f) => f > 100).toList();
  print(numberResult);
}

output:

[150, 200]

Dart list filter based on null values

There are two ways we can do it,

One way is to mutate the original list or array used removeWhere method contains a function argument, that checks null values and removes them from the original list

void main() {
  var words = ['one', null, 'three', "four"];
  words.removeWhere((value) => value == null);
  print(words); //[one, three, four]

}

In another way, the original list is not modified, But they create a new array with a list of removed values.

used the where function with checking not null values, finally returning the null values.

void main() {
  var words = ['one', null, 'three', "four"];
  List result = words.where((value) => value != null).toList();
  print(result);
}

Output:

[one, three, four]

Conclusion

This post shows how to filter and search list in multiple ways and examples