view_model library

A comprehensive ViewModel framework for Flutter applications.

This library provides a robust, scalable solution for state management in Flutter applications using the ViewModel pattern. It offers automatic lifecycle management, dependency tracking, and seamless integration with Flutter's widget system.

Quick Start

// 1. Define a ViewModel
class CounterViewModel extends ViewModel {
  int count = 0;
  void increment() => update(() => count++);
}

// 2. Create a Spec
final counterSpec = ViewModelSpec<CounterViewModel>(
  builder: () => CounterViewModel(),
);

// 3. Use in a Widget
class CounterPage extends StatefulWidget {
  @override
  State<CounterPage> createState() => _CounterPageState();
}

class _CounterPageState extends State<CounterPage>
    with ViewModelStateMixin {
  @override
  Widget build(BuildContext context) {
    final vm = viewModelBinding.watch(counterSpec);
    return ElevatedButton(
      onPressed: vm.increment,
      child: Text('Count: ${vm.count}'),
    );
  }
}

The viewModelBinding Accessor

viewModelBinding is your gateway to ViewModels:

Method Description
viewModelBinding.watch(spec) Get VM and rebuild on changes
viewModelBinding.read(spec) Get VM without rebuilding
viewModelBinding.listen(spec, onChanged:) Side effects, auto-dispose

Core Features

  • Automatic Lifecycle Management: ViewModels are automatically created, cached, and disposed based on widget lifecycle.
  • Instance Reuse: Share a single ViewModel instance across multiple widgets using keys or tags.
  • Value-level Rebuilds: Optimize performance by rebuilding only the widgets that depend on specific state values.
  • Stateful ViewModel: Manage immutable state with StateViewModel<S> and update it using setState(newState).
  • Dependency Injection: Decouple ViewModels from widgets and from each other.
  • DevTools Extension: Monitor and debug ViewModels in real-time with the Flutter DevTools extension.

Classes

AppPauseProvider
A ViewModelBindingPauseProvider that signals pause/resume based on the application's lifecycle state (AppLifecycleState).
AutoDisposeController
Controller for managing automatic disposal of resources.
ChangeNotifierViewModel
A ViewModel implementation that extends Flutter's ChangeNotifier.
Expression
Marker type to carry a code expression string in annotations. The generator will unwrap Expr(code) and emit code as an expression inside builder closures, instead of a string literal.
GenSpec
Annotation to mark a ViewModel for spec code generation. Each annotated class will receive a generated ViewModelSpec variable in a .vm.dart part file.
InstanceArg
Arguments for ViewModel instance creation and identification.
PageRoutePauseProvider
A ViewModelBindingPauseProvider that uses RouteAware to determine pause state based on route navigation events (push, pop).
StateViewModel<T>
Abstract base class for ViewModels that manage state of type T.
StateViewModelSelector<T, R>
Rebuilds from one strongly typed value selected from a StateViewModel.
StateViewModelValueWatcher<T>
A widget that listens to a StateViewModel and rebuilds itself when the selected parts of the state change.
TickerModePauseProvider
A ViewModelBindingPauseProvider that pauses/resumes based on TickerMode.
Vef
(Deprecated) Use ViewModelBinding instead.
ViewModel
Base mixin class for all ViewModels in the application.
ViewModelBinding
Core abstraction for managing ViewModel lifecycle and dependency injection.
ViewModelBindingHost
Common host interface for types exposing a viewModelBinding accessor.
ViewModelBindingInterface
Interface that exposes helpers to access ViewModels from widgets.
ViewModelBindingPauseProvider
ViewModelBindingPauseProvider controls manual pause/resume for a page/state.
ViewModelConfig
Global configuration for ViewModel behavior.
ViewModelFactory<T>
Abstract factory interface for creating ViewModel instances.
ViewModelLifecycle
Abstract interface for observing ViewModel lifecycle events.
ViewModelProvider<T extends ViewModel>
(Deprecated) Use ViewModelSpec instead.
ViewModelSpec<T extends ViewModel>
A simple, argument-less specification for creating a ViewModel. Provides a builder, an optional instance key, and an optional grouping tag. Equal non-null key values reuse one instance for the same resolved generic ViewModel type T; tag is only a grouping and lookup label.
ViewModelSpecWithArg<VM extends ViewModel, A>
A specification for creating a ViewModel from an argument. The cache identifiers are computed from the argument.
ViewModelSpecWithArg2<VM extends ViewModel, A, B>
ViewModelSpecWithArg3<VM extends ViewModel, A, B, C>
ViewModelSpecWithArg4<VM extends ViewModel, A, B, C, D>

Enums

ErrorType
The type of error reported by ViewModelConfig.onError.

Mixins

ViewModelStatelessMixin
Stateless integration for ViewModel access from widgets.
ViewModelStateMixin<T extends StatefulWidget>
Mixin that integrates ViewModels with Flutter's State lifecycle.

Constants

genSpec → const GenSpec
Shorthand constant for GenSpec.