Pattern Matching in Dart: Chapter Overview

Pattern Matching teaches you to bundle values and take data apart safely.

In this chapter, you will:

  • bundle a few values with records, and return several values from a function
  • check the shape of data with patterns, in switch and if-case
  • add when guards, and let sealed classes make every switch complete

What each lesson covers:

  • Pattern Matching: How to bundle values with records, and how patterns check the shape of a value and take it apart: in declarations, switch, when guards and if-case.

This chapter has 1 lesson. Plan on about 28 minutes, with 6 exercises to check what you learned. Every example is real Dart code: change it, run it again and compare the output, which is the fastest way to learn how each feature behaves.

Simple lesson: finish this chapter and you can read messy data with one clear line of code.

Lessons in this section

  1. Pattern Matching

    How to bundle values with records, and how patterns check the shape of a value and take it apart: in declarations, switch, when guards and if-case.

    • Create records and return several values from a function
    • Destructure records, lists, maps and objects with patterns
    • Use patterns in switch with when guards, and check data with if-case