MVVM separates a view from a view model that prepares the state and actions needed by that view. The view model should not become an all-purpose object containing every repository and navigation decision in the app.
Model states that the screen can actually show
A data list alone cannot distinguish never loaded, loading, empty success and failed loading. An explicit state representation makes those cases visible and testable.
The fragment defines immutable state variants in a Dart library. Add a feature-specific controller or view model that emits or exposes these states; the particular notification package is a separate choice.
Worked example
This is a focused code fragment. Place it in the class or file described above; it is not a complete standalone app.
sealed class LibraryState {
const LibraryState();
}
class LibraryLoading extends LibraryState {
const LibraryLoading();
}
class LibraryLoaded extends LibraryState {
const LibraryLoaded(this.titles);
final List<String> titles;
}
class LibraryFailed extends LibraryState {
const LibraryFailed(this.message);
final String message;
}
Read the result
An empty LibraryLoaded list is a successful empty result, not a failure. The view can render the states exhaustively. Keep mutable collections from changing behind an otherwise immutable state by copying or exposing an unmodifiable view.
Walk through a failed load and a successful retry
The view model exposes a loading state, calls the repository and then chooses a loaded or failed state. The view renders that result. On retry, the same sequence happens through a deliberate operation rather than a network call hidden inside build.
An empty loaded list is still success. A refresh may need to keep previously loaded data visible while work continues, which can justify a richer state than the smallest example. Decide from the actual screen contract rather than adding status flags that can form contradictory combinations.
Use unit tests to assert the sequence with a fake repository before connecting the view.
Practice and verification
Write the sequence loading → failure → retry → empty success. Verify the view model produces that sequence with a fake repository. Decide whether an already loaded list should remain visible during refresh.