I use Flutter's official approach.
How to create a view model
- View model
- State
- Repository
- Service
- Result model
Pillars of this approach.
- View model
- State manager of a widget
- The only fields of a view model can be it's state and repositories
- Functions of a view model serve as the public API that the widget can use
- Implementation in the functions of view models should only be calling repository functions and manipulation of state
- State
- Stores any sort of data passed to the view model via constructor and the mutable data (state) of the view model
- Repository
- Implementation layer of a view model
- Try to write all implementation used by view model functions within repository functions
- Service
- API calls
- Result model
- Model used to represent a result of a service