Day 19: Data Modeling with Freezed

Flutter App Development: High-Fidelity Travel App Engineering ~8 min read 60 min hands-on

Lesson 19: Data Modeling with Freezed

Module 3 — Data Modeling, Persistence & ConnectivityPart 1 of 12

Why Your Models Are Fragile Right Now

Open lib/features/search/models/flight_result.dart and look at the stopsLabel getter:

Now try to copy a FlightResult with a modified price:

That is fourteen fields you have to remember to copy manually. Forget one, and you silently corrupt the object. There's no == check — every object is a different identity even with identical fields. No toString that shows you what changed during debugging.

FlightResult is a data transfer object — it maps the API wire format. But your app logic shouldn't talk to the wire format directly. This lesson introduces the domain layer: a set of clean, immutable models that represent your application's business concepts, completely independent of any API.

The Architecture: Two Model Layers

The conversion happens exactly once, at the boundary where your Provider fetches data from the repository. Everything above the repository — your screens, your notifiers, your tests — talks to the domain layer.

Why not just use FlightResult everywhere?

  1. FlightResult has 15 required constructor parameters — no defaults, no copyWith.

  2. It depends on CabinClass from the search feature. Your booking logic shouldn't depend on search.

  3. In Lesson 25, when you swap the mock API for the real Amadeus API, the FlightResult shape might change. If your screens reference FlightResult directly, you have to update every screen. With a domain layer, you only update the converter.

Introducing Freezed

Freezed is a Dart code generator. You annotate your class with @freezed, run build_runner, and the generator writes the boilerplate:

  • copyWith — create a modified copy without mutation

  • == and hashCode — value equality based on all fields

  • toString — readable debug output

  • Union types — sealed variant handling with when / map

You write the interesting parts. The generator writes the tedious parts.

Setting Up

Two new entries in pubspec.yaml:

And one analysis_options.yaml change to suppress the annotation warning:

The Domain Models

TravelClass — No Freezed Needed

Notice that TravelClass doesn't import from CabinClass — only fromCabinClass does. The domain enum is independent; the API enum is only referenced in the converter. When you replace the API layer in Lesson 25, TravelClass doesn't change.

Flight — The Primary Domain Model

The private constructor const Flight._(); is critical. Without it, Freezed's generated _$Flight mixin cannot call super._(), which means the class body can't define custom getters. This is a common gotcha — the pattern is: annotate with @freezed, add const ClassName._(), then write your factory.

Now copyWith works automatically:

And equality is value-based:

PriceBreakdown — Computed Total

total is a computed getter, not a stored field. Because the class is immutable, the result is always consistent with the fields that define it. You never have breakdown.total disagree with breakdown.baseFare + breakdown.taxes.

BookingState — Union Type

This is where Freezed's power becomes obvious. A booking flow has four distinct states that carry completely different data:

Freezed generates when, maybeWhen, map, and maybeMap dispatch methods:

This is exhaustive — the compiler enforces that every case is handled. No forgotten else branches. No runtime surprises.

Running build_runner

After flutter pub get, Freezed's annotation processor needs to run:

This generates one .freezed.dart file per annotated class:

Keep going with the full lesson

The rest of this lesson — plus its implementation guide, runnable scripts and source code — is included with the course or an All Access plan. Create a free account to start the free lessons in every course.

Already a member? Sign in