Sub topic 1
What MVVM Means in a Flutter App
Prerequisites
This chapter assumes basic Dart syntax, including classes, async and await, and Future. You should also know Flutter widgets such as StatelessWidget, StatefulWidget, and build. Familiarity with setState is useful because MVVM moves some responsibilities out of a widget.
The MVVM idea
MVVM stands for Model-View-ViewModel. It divides an application into parts with different responsibilities:
| Part | Responsibility | Flutter example |
|---|---|---|
| Model | Represents data and data access | `Todo`, `TodoRepository` |
| View | Displays state and receives input | `TodoScreen` |
| View model | Holds UI state and coordinates actions | `TodoListViewModel` |
The view should describe what the user sees. It should not decide how a network request works or contain rules for updating a todo. The view model exposes the state needed by the view and provides actions the view can call.
A repository is an abstraction over data access. It can load data from a service, database, or in-memory collection. A service usually wraps one external source, such as an HTTP API. The repository can combine or select data from one or more services and present a stable interface to the view model.
The direction of communication is intentional:
Why separate these roles?
A small screen can work with local variables and setState. As a screen grows, the widget may accumulate loading flags, error handling, validation, filtering, and data-access calls. The build method becomes difficult to read and test.
MVVM keeps the widget focused on rendering. The view model owns decisions such as:
- when loading starts and stops;
- which data the view receives;
- what happens when a user toggles an item;
- which error state should be displayed.
The repository owns data access. This boundary means the view model does not need to know whether todos came from REST, SQLite, or a test fake.
A small working model
A view model commonly extends ChangeNotifier. It stores private mutable fields and exposes public getters. When an operation changes state, it calls notifyListeners(). A listening widget then rebuilds.
class TodoListViewModel extends ChangeNotifier {
TodoListViewModel(this.repository);
final TodoRepository repository;
List<Todo> todos = const [];
bool isLoading = false;
Object? error;
Future<void> load() async {
isLoading = true;
error = null;
notifyListeners();
try {
todos = await repository.fetchTodos();
} catch (exception) {
error = exception;
} finally {
isLoading = false;
notifyListeners();
}
}
}In production code, expose read-only getters rather than public mutable fields. The view can then observe state without changing it directly. ListenableBuilder or the provider package can connect the view model to the widget tree.
Exercise
Exercise
A button in a todo screen directly calls an HTTP client and then changes several widget fields. Which MVVM responsibility is misplaced, and where should it move?
Show answer
The data access belongs behind a repository, and the operation and resulting UI state belong in the view model. The view should call a view-model action and render the resulting state.