{

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 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 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

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

How to iterate loop Enum in Dart | Get Enum with index in Flutter By Example

Dart Enum comparison operator| Enum compareByIndex example| Flutter By Example

Dart tutorial examples/ Flutter By Examples

How to add Methods or Values to Enums in Dart | Flutter By Example

How to convert Double to Integer or Integer to double in Dart| Flutter By Example

How to: Enum to String in Dart | Print Enum name as String in Flutter By Example

Dart| Flutter How to get File name and extension with example