Dart: Find List Elements that Satisfy Conditions

Last updated on May 18, 2021 Guest Contributor Loading... Post a comment

When developing apps with Flutter and Dart, you’ll be working a lot with lists. One of the most common tasks when working with lists is finding elements that satisfy single or multiple conditions. This article walks you through a few examples of how to get that task done. We’ll use the where() method from the Iterable class. This method will return a new Iterable without changing the original list.

Search from a List of Numbers/Strings

Example 1.1

The program below will find all numbers greater than 3 and less than 9 from a given list.

void main() {
  final List<int> numbers = [1, 2, 10, 9, 11, 4, 5, 7, 2];
  final List<int> results =
      numbers.where((element) => element < 9 && element > 3).toList();
  print(results);
  runApp(MyApp());
}

Output:

[4, 5, 7]

Example 1.2

This program will return a list of names that start with a capital “B”.

void main() {
  final List<String> names = [
    'John Doe',
    'Bobby Right',
    'Voldermort',
    'Pennywise The Clown',
    'Bill Clinton'
  ];

  final List<String> namesStartWithB =
      names.where((element) => element.startsWith('B')).toList();
  print(namesStartWithB);
}

Output:

[Bobby Right, Bill Clinton]

Search from a List of Objects

This example finds all products that cost less than 100 and get free shipping.

import 'package:flutter/foundation.dart';

/// Define how a product looks like
class Product {
  String name;
  double price;
  bool isFreeShip;

  Product(
      {@required this.name, @required this.price, @required this.isFreeShip});
}

void main() {
  // The list of all products
  final List<Product> _allProducts = [
    Product(name: 'Book', price: 9, isFreeShip: false),
    Product(name: 'Cooking Pan', price: 81.29, isFreeShip: true),
    Product(name: 'DVD', price: 6.99, isFreeShip: false),
    Product(name: 'Laptop', price: 500, isFreeShip: true),
    Product(name: 'Shoes', price: 48.99, isFreeShip: true)
  ];

  final _results = _allProducts
      .where((product) => product.price < 100 && product.isFreeShip == true);

  for (Product p in _results) {
    print(p.name);
  }
}

Output:

Cooking Pan
Shoes

Search from a List of Maps

This example will find all developers who have experience with Flutter:

// main.dart

const List<Map<String, dynamic>> developers = [
  {
    "name": "Dev A",
    "skills": ["Flutter", "Node.js", "Swift UI", "Java"]
  },
  {
    "name": "Dev B",
    "skills": ["Flutter", "SQL", "Kotin", "Python"]
  },
  {
    "name": "Dev C",
    "skills": ["Xamarin", "React Native", "SQL"]
  }
];

void main() {
  final List<Map<String, dynamic>> d = developers
      .where((developer) => developer["skills"].contains("Flutter"))
      .toList();

  print(d);
}

Output:

[
  {name: Dev A, skills: [Flutter, Node.js, Swift UI, Java]}, 
  {name: Dev B, skills: [Flutter, SQL, Kotin, Python]}
]

Conclusion

You’ve learned how to get all elements that match one or many given conditions from a list. If you’d like to explore more new and exciting things about Dart and Flutter then take a look at the following articles: Conditional (Ternary) Operator in Dart and FlutterSorting Lists in Dart and Flutter (5 Examples)Flutter + Firebase Storage: Upload, Retrieve, and Delete filesUsing GetX (Get) for State Management in FlutterFlutter Autocomplete example.

You can also take a tour around our Flutter topic page, or Dart topic page for the latest tutorials and examples.

Related Articles

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x