view_model 0.4.3+1
view_model: ^0.4.3+1 copied to clipboard
view model for flutter. Simple and Lightweight & Cross - Widget Sharing & Automatic Resource Disposal
0.4.3 #
- Add
maybeWatchViewModel
andmaybeReadViewModel
- update
watchViewModel
find logic
VM watchViewModel<VM extends ViewModel>({
ViewModelFactory<VM>? factory,
String? key,
});
Parameter Name | Type | Optional | Description |
---|---|---|---|
factory |
ViewModelFactory<VM>? |
✅ | Provides the construction method for the ViewModel. Optional; if an existing instance is not found in the cache, it will be used to create a new one. |
key |
String? |
✅ | Specifies a unique key to support sharing the same ViewModel instance. First, it tries to find an instance with the same key in the cache. |
tag |
Object? |
✅ | Add a tag for ViewModel instance. get tag by viewModel.tag . and it's used by find ViewModel by watchViewModel(tag:tag) . |
🔍 Lookup Logic Priority (Important)
The internal lookup and creation logic of watchViewModel
is as follows (executed in priority
order):
- If a key is passed in:
- First, attempt to find an instance with the same key in the cache.
- If a factory exists, use the factory to get a new instance.
- If no factory is found and no instance is found, an error will be thrown.
- If a tag is passed in, attempt to find the latest created instance which has the same tag in the cache.
- If nothing passed in, attempt to find the latest created instance of this type in the cache.
⚠️ If no ViewModel instance of the specified type is found, an error will be thrown. Ensure that the ViewModel has been correctly created and registered before use.
0.4.2 #
- Support find existing ViewModel by tag
set tag in ViewModelFactory.getTag()
:
class MyViewModelFactory extends ViewModelFactory<MyViewModel> {
@override
Object? getTag() {
return 'tag';
}
}
find existing ViewModel by tag:
MyViewModel get viewModel => watchViewModel<MyViewModel>(tag: 'tag');
0.4.1 #
Breaking change:
- Use
recycleViewModel
instead ofrefreshViewModel
.
0.4.0 #
Breaking change:
-
Use
ViewModel
instead ofStatelessViewModel
. -
Use
StateViewModel
instead ofViewModel
. -
Use either
watchViewModel
orreadViewModel
instead ofgetViewModel
/requireExistingViewModel
. -
Use
StateViewModel.listenState
instead ofViewModel.listen
. -
Use
ViewModel.listen
instead ofViewModel.addListener
. -
Support
ViewModel.read<T>
to read existing view model globally.
0.3.0 #
- transfer to https://github.com/lwj1994/flutter_view_model. thank to Miolin