CodeBlanks Logo Icon CodeBlanks

Putting it together

Combine all parts into a working final app.

Putting it together

In the final lesson we combine all the parts: the Library class, the Book class, the book list and error handling with try/catch. We will get a working library console application; it prints all books and gives a friendly message on error.

This combines all the beginner fundamentals in a real task: class definition, constructor, field access, List and a for loop, resilience with try/catch. You will see this pattern again and again in later projects; so learning it well now matters.

The working app

The code below already has the classes, the list and try/catch. The only missing piece is the for loop that prints all books. You will add this loop before the try block; so first all books are listed, then the out-of-range access is tried.

When you write for (final book in books) { print("Book: ${book.title}"); } each book's title appears. try/catch is already in place; so the app both lists and catches errors. This is a small but complete example of the beginner level.

Practice Working library

The classes, the list and try/catch are already defined in the code below. In main add a for loop before the try block: for (final book in books) { print("Book: ${book.title}"); }. So first all books are printed, then the out-of-range access is tried.

Starter code

main.dart
class Library {
  String name;
  Library(this.name);
  void info() {
    print("Library: $name");
  }
}

class Book {
  String title;
  Book(this.title);
}

void main() {
  final lib = Library("City");
  lib.info();
  final b = Book("Crime and Punishment");
  print("New book: ${b.title}");
  final books = <Book>[Book("Crime and Punishment"), Book("Les Mis")];
  print("Count: ${books.length}");
  print("First: ${books[0].title}");
  // print all books with a for loop before try
  try {
    print(books[5].title);
  } catch (e) {
    print("book not found");
  }
}

Sample solution

main.dart
class Library {
  String name;
  Library(this.name);
  void info() {
    print("Library: $name");
  }
}

class Book {
  String title;
  Book(this.title);
}

void main() {
  final lib = Library("City");
  lib.info();
  final b = Book("Crime and Punishment");
  print("New book: ${b.title}");
  final books = <Book>[Book("Crime and Punishment"), Book("Les Mis")];
  print("Count: ${books.length}");
  print("First: ${books[0].title}");
  for (final book in books) {
    print("Book: ${book.title}");
  }
  try {
    print(books[5].title);
  } catch (e) {
    print("book not found");
  }
}

Expected output

Library: City
New book: Crime and Punishment
Count: 2
First: Crime and Punishment
Book: Crime and Punishment
Book: Les Mis
book not found

Questions and answers

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

1 The for loop connector

Question
for (final book ____ books) {
  print("Book: ${book.title}");
}
Answer
for (final book in books) {
  print("Book: ${book.title}");
}

Explanation: for-in is the list-walking idiom.

2 What does the for loop do in this project?

main.dart
for (final book in books) {
  print("Book: ${book.title}");
}
  • Prints every book's title Correct answer
  • Adds a book to the list
  • Catches the error
  • Changes the library name

Explanation: the for loop prints every book's title.

3 Match the final parts

Term Meaning
class defines an object
List<Book> gathers books
for walks the list
try/catch adds resilience

All lessons in Beginner