Sub topic 11
User actions flow in, state flows out
This chapter assumes basic Dart classes, async/await, Flutter widget builds, and the earlier Todo, TodoRepository, and ChangeNotifier examples. The key idea is unidirectional data flow: the view sends user actions to the view model, and the view model sends updated state back to the view.
The direction of data flow
A widget should describe what the UI looks like for the current state. It should not decide how a todo is saved or change the todo list directly.
The flow is:
- The user taps a checkbox.
- The view calls a method on the view model.
- The view model validates and performs the action through the repository.
- The view model updates its private state.
- The view model calls
notifyListeners. - The view rebuilds from the new public state.
This gives every layer one direction of responsibility:
| Layer | Receives | Produces |
|---|---|---|
| View | View-model state | User actions |
| View model | User actions and repository results | UI state |
| Repository | Data requests | Data or errors |
Put actions on the view model
The view model exposes an action method instead of exposing its mutable list. The view can request a change, but it cannot apply the change itself.
class TodoListViewModel extends ChangeNotifier {
TodoListViewModel({required TodoRepository repository})
: _repository = repository;
final TodoRepository _repository;
List<Todo> _todos = const [];
Object? _error;
List<Todo> get todos => _todos;
Object? get error => _error;
Future<void> toggleTodo(Todo todo) async {
_error = null;
notifyListeners();
try {
await _repository.setDone(todo.id, !todo.done);
_todos = await _repository.fetchTodos();
} catch (error) {
_error = error;
}
notifyListeners();
}
}The repository remains the source of truth. After saving, the view model fetches the list again. This is simple and reliable when the repository might apply rules, normalize data, or receive changes from a server.
The view forwards the action:
CheckboxListTile(
title: Text(todo.title),
value: todo.done,
onChanged: (_) {
viewModel.toggleTodo(todo);
},
)The view does not need to await the Future. It observes isLoading, error, or other state properties and renders the appropriate result.
Handling repeated actions
Asynchronous actions can overlap. A user might tap a checkbox several times before the first request finishes. Without protection, responses can arrive in an unexpected order.
Choose a policy for each action:
- Disable the control while its request is running.
- Track busy todo IDs and ignore repeated taps.
- Queue actions when every action must be preserved.
- Use request IDs when only the newest result matters.
Do not hide this policy inside the widget. Keep it in the view model so tests can verify it.
Exercise
A checkbox tap should update a todo immediately, but the repository request can fail. Which layer should perform the update and decide what the UI displays after failure?
Show answer
The view model should perform the repository request, update its state, store any error, and notify listeners. The view should only forward the tap and render the resulting state.