Sub topic 1
Why flags and nullable fields allow impossible states
A screen often has a small number of meaningful states. For example, an item list can be loading, loaded with items, loaded with no items, or failed.
A common first design stores these states as separate fields:
class ItemsState {
ItemsState({
required this.isLoading,
this.items,
this.errorMessage,
});
final bool isLoading;
final List<String>? items;
final String? errorMessage;
}This class looks flexible, but it accepts combinations that do not describe a real situation. It can represent all of these:
- Loading with items from an earlier request.
- Loading and failed at the same time.
- Neither loading nor failed, with no items.
- Failed with both items and an error.
- Not loading, with an empty or missing result.
The type system cannot tell which combinations your application permits. Every field is independently valid, so the class allows invalid combinations by design.
Prerequisites
This chapter assumes that you know Dart classes, constructors, final fields, null safety, basic generics, and Flutter's build method. You also need Dart 3 or later for sealed classes and the pattern syntax used later in this tutorial.
Why nullable fields create ambiguity
A nullable field has two possible meanings. For example, items == null might mean:
- The request has not started.
- The request is still running.
- The request failed.
- The server returned no data.
The field itself does not record which meaning applies. Code elsewhere must infer the meaning from other fields:
String message(ItemsState state) {
if (state.isLoading) {
return 'Loading...';
}
if (state.errorMessage != null) {
return state.errorMessage!;
}
if (state.items != null) {
return '${state.items!.length} items';
}
return 'Unknown state';
}The final branch is evidence that the model is incomplete. It handles a combination that should probably never occur, but the compiler cannot distinguish an invalid state from a valid one.
The non-null assertion operator (!) adds another risk. It tells Dart that a nullable value is definitely present. If the program's flags are inconsistent, the assertion can fail at runtime.
Flags make transitions harder to reason about
State usually changes in response to events such as starting a request, receiving data, or receiving an error. With independent fields, each transition must remember to reset unrelated fields.
ItemsState startLoading(ItemsState old) {
return ItemsState(
isLoading: true,
items: old.items,
errorMessage: null,
);
}Keeping old items while loading might be intentional, but the model does not express that decision. Another developer could set items to null, leave an old error in place, or forget to clear a field. Each choice creates a different combination.
A state model should make invalid combinations difficult or impossible to construct. One way to do that is to represent each valid state as a separate subtype. A loading value then cannot also carry an error unless the design explicitly gives it that data.
A sealed class hierarchy addresses this problem by defining a closed set of alternatives. The alternatives are checked by the compiler when you handle them. The next chapter replaces the field bag with one type per valid state.
Exercise
What impossible state can the ItemsState class represent when isLoading is true, items is non-null, and errorMessage is also non-null?
Show answer
It represents loading, successful data, and failure at the same time. The class accepts this combination because its fields are independent.