Sub topic 4
Designing the State a View Needs
Assume you know basic Dart classes, async and await, Flutter widget lifecycles, and how setState works. This chapter focuses on the state contract between a view model and its view.
Start with the view's questions
A view should not need to understand repository calls or business rules. It needs enough state to answer questions such as:
- Should I show a progress indicator?
- Which todos should I display?
- Should I show an error message?
- Can the user tap this action now?
Represent these answers as one immutable state object. Immutable means that an existing state object does not change after it is created. The view model replaces it with a new object and then notifies listeners.
| State field | Meaning |
|---|---|
| `todos` | The items currently displayed |
| `isLoading` | An asynchronous operation is running |
| `errorMessage` | A user-visible failure, or `null` |
Model states explicitly
A boolean such as isLoading is useful, but it does not describe every situation. For example, the view may have old data while a refresh runs. It may also have an error and cached data at the same time.
Keep the state rules clear. The following model preserves existing todos during loading and failure:
class Todo {
const Todo({required this.id, required this.title, this.done = false});
final int id;
final String title;
final bool done;
}
class TodoUiState {
TodoUiState._({
required List<Todo> todos,
required this.isLoading,
required this.errorMessage,
}) : todos = List.unmodifiable(todos);
const TodoUiState.initial()
: todos = const [],
isLoading = false,
errorMessage = null;
factory TodoUiState.loading(List<Todo> todos) => TodoUiState._(
todos: todos,
isLoading: true,
errorMessage: null,
);
factory TodoUiState.ready(List<Todo> todos) => TodoUiState._(
todos: todos,
isLoading: false,
errorMessage: null,
);
factory TodoUiState.failure(String message, List<Todo> todos) =>
TodoUiState._(
todos: todos,
isLoading: false,
errorMessage: message,
);
final List<Todo> todos;
final bool isLoading;
final String? errorMessage;
}List.unmodifiable prevents callers from changing the list behind the view model's back. The view can read todos, but it cannot mutate the state directly.
Define transitions before writing widgets
A load operation can follow this sequence:
The view model owns these transitions. The view only renders them:
initial: show an empty view or placeholder.loading: show progress while optionally keeping current data visible.ready: show the current todos.failure: show an error and, when possible, keep useful old data visible.
This design avoids scattering flags across widgets. A widget should not infer that “not loading” means “success.”
Avoid state that the view can derive
Store facts that come from the model or an operation. Derive simple display values instead. For example, the view can calculate whether todos.isEmpty, rather than the view model storing both todos and hasTodos. Duplicate state can become inconsistent.
Exercise
Why might a view model preserve old todos while a refresh is loading or has failed?
Show answer
Preserving them prevents the screen from becoming blank during refresh and gives the user useful data when the new request fails.