CodeBlanks Logo Icon CodeBlanks

Search and filter

Write search and filtering with collection methods.

The search method

To search books we write a search method. It takes a query string and returns books whose title contains that text. It uses where and contains.

Example: List<Book> search(String query) { return books.where((b) => b.title.contains(query)).toList(); }. Here where filters each book; b.title.contains(query) checks whether the title has the query text.

The result is a List<Book>. toList() turns the lazy Iterable into a concrete list. This method lets a user find books by a keyword.

Search result

The search method returns a List<Book>. The length of the result tells how many books were found. For example if two of three books contain the query then results.length is 2.

Example: var results = lib.search('and');. If 'Crime and Punishment' and 'War and Peace' are added both contain 'and'; the result is 2 books. print(results.length) gives 2.

This method is the most useful feature of the library app for users. It gives results in seconds among thousands of books.

Practice Add the search method

Add a List<Book> search(String query) method to the Library class. The method should return books.where((b) => b.title.contains(query)).toList(). The main already adds three books and calls lib.search('and'); it prints the length of the result.

Starter code

main.dart
class Book {
  String title;
  bool borrowed = false;
  Book(this.title);
}

class Library {
  List<Book> books = [];
  void addBook(Book b) {
    books.add(b);
  }
  void checkout(int index) {
    books[index].borrowed = true;
  }
  void returnBook(int index) {
    books[index].borrowed = false;
  }
  // add the search method
}

void main() {
  var lib = Library();
  lib.addBook(Book('Les Miserables'));
  lib.addBook(Book('Crime and Punishment'));
  lib.addBook(Book('War and Peace'));
  var results = lib.search('and');
  print(results.length);
}

Sample solution

main.dart
class Book {
  String title;
  bool borrowed = false;
  Book(this.title);
}

class Library {
  List<Book> books = [];
  void addBook(Book b) {
    books.add(b);
  }
  void checkout(int index) {
    books[index].borrowed = true;
  }
  void returnBook(int index) {
    books[index].borrowed = false;
  }
  List<Book> search(String query) {
    return books.where((b) => b.title.contains(query)).toList();
  }
}

void main() {
  var lib = Library();
  lib.addBook(Book('Les Miserables'));
  lib.addBook(Book('Crime and Punishment'));
  lib.addBook(Book('War and Peace'));
  var results = lib.search('and');
  print(results.length);
}

Expected output

2

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 The where call

Question
List<Book> search(String query) {
  return books.____((b) => b.title.contains(query)).toList();
}
Answer
List<Book> search(String query) {
  return books.where((b) => b.title.contains(query)).toList();
}

Explanation: where filters by a condition.

2 What does b.title.contains(query) return?

main.dart
var b = Book('Crime and Punishment');
var r = b.title.contains('Punishment');
  • A list
  • void
  • It throws
  • bool (true or false) Correct answer

Explanation: contains returns a bool: true if found, false otherwise.

3 Match the search parts

Term Meaning
String query Query parameter
.where(...) Filter
.contains(query) Search in title
.toList() Convert to list

All lessons in Intermediate