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.
| Concept | What it means | Decision rule |
|---|---|---|
| Generic parameter | A type placeholder preserved through an API | Use when behavior is identical across types and type relationships matter |
| Bound | A constraint on acceptable type arguments | Add only capabilities the implementation truly requires |
| Record | A structurally typed fixed group of values | Use 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.
- Define the generic paged-result contract boundary: user goal, inputs, visible states, ownership, and expected failures.
- Build the smallest working vertical slice with typed data and explicit dependencies.
- Represent loading, empty, success, and failure behavior where the feature can encounter them.
- Verify logic away from the UI, then exercise the rendered behavior at its public boundary.
- Inspect lifecycle, accessibility, performance, security, and platform behavior before widening the feature.
- Refactor only after behavior is protected by repeatable evidence.
Protect the frame
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.
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
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
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