ViewModel class abstract

ViewModels are essentially UI State Controllers

A ViewModel lives until the "Scope" of it is disposed which is typically a page or a tab. See ViewModelScope Inside a ViewModelScope, you can access a ViewModel inside a Widget like this

@override
Widget build(BuildContext context) {
  final CounterViewModel viewModel = context.viewModelProvider.get();
}

ViewModels allow for UI Widgets to be as simple as possible UI only talks and listens to the ViewModel, the rest of the application code is abstracted away.

                              ┌─────────┐
                 ┌───────────►│ Network │
                 │            └─────────┘
┌────┐     ┌─────┴─────┐      ┌──────────┐
│ UI │◄───►│ ViewModel ├─────►│ Database │
└────┘     └─────┬─────┘      └──────────┘
                 │            ┌──────────┐
                 └───────────►│ MemCache │
                              └──────────┘

One can choose to have more abstractions between the ViewModel and network, database and memcache like Repository, Service to simplify the logic inside a ViewModel or to hold some state in a larger scope than the ViewModel

Constructors

ViewModel()

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
onDispose() → void
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited