Phase 1 · Dart FoundationsModule 9~46 min read

Interfaces, Mixins & Class Modifiers

Build expressive type hierarchies with implicit interfaces, mixins, extensions, sealed types, and deliberate composition.

What you'll learn

Build expressive type hierarchies with implicit interfaces, mixins, extensions, sealed types, and deliberate composition. The lesson turns the APIs into a repeatable engineering workflow instead of a collection of isolated snippets.

By the end of this lesson, you'll be able to:

  • Apply Interfaces in a production-shaped Flutter feature
  • Apply Inheritance in a production-shaped Flutter feature
  • Apply Abstract types in a production-shaped Flutter feature
  • Apply Mixins and extensions in a production-shaped Flutter feature

Core mental model

Connect each API to the decision it supports. Flutter code stays maintainable when state, ownership, lifecycle, and platform boundaries are explicit.

ConceptWhat it meansDecision rule
Implicit interfaceEvery class exposes an implementable interfaceDepend on the smallest behavior contract a client needs
MixinReusable implementation applied across compatible classesUse for orthogonal behavior, not primary identity
Sealed classA closed subtype family within one libraryUse when every state variant should be handled exhaustively

Professional workflow

Work in small vertical slices and keep behavior observable from the first iteration.

  1. Define the sealed learning-state model boundary: user goal, inputs, visible states, ownership, and expected failures.
  2. Build the smallest working vertical slice with typed data and explicit dependencies.
  3. Represent loading, empty, success, and failure behavior where the feature can encounter them.
  4. Verify logic away from the UI, then exercise the rendered behavior at its public boundary.
  5. Inspect lifecycle, accessibility, performance, security, and platform behavior before widening the feature.
  6. Refactor only after behavior is protected by repeatable evidence.

Protect the frame

Keep build methods predictable, move side effects to explicit owners, and measure before introducing caches, isolates, or architectural layers.

Guided Flutter lab

Build a focused sealed learning-state model slice

This compact example keeps the important ownership and data-flow decisions visible so the behavior is easy to extend and test.

lib/load_state.dart
sealed class LoadState<T> { const LoadState(); }
class Idle<T> extends LoadState<T> { const Idle(); }
class Loading<T> extends LoadState<T> { const Loading(); }
class Ready<T> extends LoadState<T> { const Ready(this.value); final T value; }
class Failed<T> extends LoadState<T> { const Failed(this.message); final String message; }

String label<T>(LoadState<T> state) => switch (state) {
  Idle() => 'Not started',
  Loading() => 'Loading',
  Ready() => 'Ready',
  Failed(message: final message) => message,
};

Production practice

Contract

Define the sealed learning-state model inputs, outputs, owner, lifecycle, visible states, and platform assumptions before selecting APIs or packages.

Verification

Protect pure rules with unit tests and the rendered public contract with widget or integration evidence; include one unavailable or failure case.

Operations

Keep dependencies replaceable, log actionable context without user secrets, and measure user-visible behavior before optimizing.

Common failure mode

Deep inheritance binds unrelated change pressures together; prefer composition and introduce a hierarchy only when substitutability is real.

Independent workshop

Extend the guided lab into a review-ready sealed learning-state model feature that fits the running course portfolio app.

Your finished workshop must include:

  • Interfaces
  • Inheritance
  • Abstract types
  • Mixins and extensions
  • Class modifiers
  • Automated verification and a short design note

Definition of done

Demonstrate the happy path, an empty or unavailable state, and at least one failure path. Add an automated check and a short note explaining one design decision.

Recap & quick check

Key takeaways

  • Implicit interface: Depend on the smallest behavior contract a client needs
  • Mixin: Use for orthogonal behavior, not primary identity
  • Sealed class: Use when every state variant should be handled exhaustively

Quick check

1. Which rule best applies to Implicit interface?

2. Which rule best applies to Mixin?

3. Which rule best applies to Sealed class?

Next: Generics, Records & Advanced Types