Sub topic 2
Destructuring state with object patterns
This chapter assumes you know Dart 3 classes, null safety, generics, and basic Flutter widgets with build methods. You should have a sealed hierarchy like the one from the previous chapter.
An object pattern checks that a value has a given type and pulls its fields into local variables in one step. In Loaded(data: final items), Loaded is the type check and data: final items reads the data field into items. Patterns work in switch expressions, if-case, and destructuring assignments.
Start with a sealed state type that holds one of three variants:
sealed class LoadState<T> {
const LoadState();
}
final class Loading<T> extends LoadState<T> {
const Loading();
}
final class Loaded<T> extends LoadState<T> {
const Loaded(this.data);
final T data;
}
final class Failed<T> extends LoadState<T> {
const Failed(this.error);
final Object error;
}Letting the compiler check every case
A switch expression over a sealed type must cover every variant. If you add a new variant and forget a case, the code does not compile. This is exhaustiveness checking. You get the check for free only when the switch has no default branch.
String describe(LoadState<List<String>> state) => switch (state) {
Loading() => 'Loading...',
Loaded(data: final items) when items.isEmpty => 'Nothing here yet',
Loaded(data: final items) => '${items.length} items',
Failed(error: final e) => 'Error: $e',
};The when guard on the empty-list case narrows the Loaded branch. The compiler accepts the second Loaded branch as covering the rest of the lists.
Reusing one state type for different data
LoadState<T> is generic, so one state type serves any payload. LoadState<List<String>> carries a list of names, and LoadState<User> carries a single user. Each use site fixes T, so the compiler knows the type of data in every branch.
Turning state into widgets
A widget can return a different subtree for each state in one switch expression. Because the switch is exhaustive, a new state breaks the build until you handle it in the UI.
import 'package:flutter/material.dart';
class ItemsView extends StatelessWidget {
const ItemsView({super.key, required this.state});
final LoadState<List<String>> state;
@override
Widget build(BuildContext context) {
return switch (state) {
Loading() => const Center(child: CircularProgressIndicator()),
Loaded(data: final items) => ListView(
children: [for (final item in items) ListTile(title: Text(item))],
),
Failed(error: final e) => Center(child: Text('Something went wrong: $e')),
};
}
}> Note on the diagram: the widget above handles only the three variants shown, so it breaks at compile time if you add a fourth variant without a matching branch.
Exercise
The describe function has no default branch. What happens if you add final class Retrying<T> extends LoadState<T> {} and do not update describe?
Show answer
The code does not compile, because the switch expression no longer covers every subtype of the sealed LoadState class. The compiler reports the missing case, so you must add a Retrying() branch before the code runs.