view_model
1.3.1, released 5 weeks ago by pub.lwjlol.com
A view model library with automatic lifecycle management and dependency resolution.
What it does
Manage state, lifecycle, and dependency composition using ViewModels and bindings. It prevents memory leaks and shares state across widgets or plain Dart classes without needing a widget tree.
AI-written summary of version 1.3.1, based on the package's README and metadata. Report an inaccuracy
Use it when
- You want automatic lifecycle management and disposal for view models.
- You need to share state and manage dependencies outside the widget tree.
- You want explicit ownership models for view model instances.
Skip it when
- You prefer stream-based architecture like BLoC.
- You are already heavily invested in Provider or Riverpod for your architecture.
Strengths
- Independent of the widget tree, usable in plain Dart classes.
- Automatic lifecycle management prevents memory leaks.
- Supports composition of modules through ownership bindings.
Weaknesses
- Global registry and configuration are process-global, requiring sequential tests.
- Requires understanding specific spec and binding rules.
Setup
- Kind
- Flutter widgets
- Effort
- Low
- Code generation
- No
- Direct dependencies
- 3
Optionally enable the view_model_lint analyzer plugin in analysis_options.yaml.
import 'package:view_model/view_model.dart';
class CounterViewModel with ViewModel {
int count = 0;
void increment() => update(() {
count += 1;
});
}
final counterSpec = ViewModelSpec<CounterViewModel>(
builder: CounterViewModel.new,
);Alternatives
Packages that do the same job and could replace view_model.
- riverpod
If you prefer a compile-safe, provider-based dependency injection and state management solution.
99Compare - bloc
If you prefer a strict, predictable event-to-state stream-based architecture.
92Compare - provider
If you want a lightweight and traditional Flutter dependency injection and state propagation tool.
81Compare
Works with
Packages commonly used together with view_model: ones it builds on, ones built on it, and its own family.