{

Dart| Flutter How to: List Filter search with examples


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

Dart and Flutter Fliter list examples

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 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 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 the create a new array with a list of removed values.

used 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

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.





Related posts

Dart| Flutter How to: Read pubspec.yaml attributes (version) with examples

How to Create and build a Singleton Class Dart or Flutter example| Dart By Example

What is the difference between var and dynamic and final in dart?| Flutter By Example

How to: Check if Phone number is valid or not in Dart| Flutter By Example

Dart| Flutter How to read an image from a disk and resize a file

Dart/Flutter: How to write setter and getter fields or members variables in a class with Example

Dart| Flutter How to get extension name and MIME type of a file with example