Phase 2 · Modern & Asynchronous DartModule 10~44 min read

Generics, Records & Advanced Types

Write reusable type-safe APIs with bounded generics, structural records, multiple return values, and precise aliases.

What you'll learn

Write reusable type-safe APIs with bounded generics, structural records, multiple return values, and precise aliases. 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 Generic APIs in a production-shaped Flutter feature
  • Apply Bounds in a production-shaped Flutter feature
  • Apply Records in a production-shaped Flutter feature
  • Apply Structural typing 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
Generic parameterA type placeholder preserved through an APIUse when behavior is identical across types and type relationships matter
BoundA constraint on acceptable type argumentsAdd only capabilities the implementation truly requires
RecordA structurally typed fixed group of valuesUse for small local data bundles; use a class when behavior or identity grows

Professional workflow

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

  1. Define the generic paged-result contract 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 generic paged-result contract slice

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

lib/page.dart
typedef Page<T> = ({List<T> items, String? nextCursor});

Page<R> mapPage<T, R>(Page<T> page, R Function(T) convert) => (
  items: page.items.map(convert).toList(growable: false),
  nextCursor: page.nextCursor,
);

final result = mapPage<int, String>(
  (items: [1, 2], nextCursor: 'page-2'),
  (value) => 'Lesson $value',
);

Production practice

Contract

Define the generic paged-result contract 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

Using Map<String, dynamic> as an all-purpose record discards field names, types, and exhaustiveness that records or domain classes can preserve.

Independent workshop

Extend the guided lab into a review-ready generic paged-result contract feature that fits the running course portfolio app.

Your finished workshop must include:

  • Generic APIs
  • Bounds
  • Records
  • Structural typing
  • Multiple returns
  • 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

  • Generic parameter: Use when behavior is identical across types and type relationships matter
  • Bound: Add only capabilities the implementation truly requires
  • Record: Use for small local data bundles; use a class when behavior or identity grows

Quick check

1. Which rule best applies to Generic parameter?

2. Which rule best applies to Bound?

3. Which rule best applies to Record?

Next: Errors, Exceptions & Result Modeling