get<T extends ViewModel> method
Returns the ViewModel
present in the scope or creates one
If the ViewModel
is already present in the scope then it just returns it
So this method is safe to call within Widget.build()
ViewModel
in the scope is identified using it's type by default
If you want multiple ViewModel
s of the same type in a scope, then
you can use the key
parameter to distinguish the different ViewModel
s
of the same type
Implementation
T get<T extends ViewModel>({String key = ""}) {
final T? viewModel = viewModelStore.get<T>(key: key);
if (viewModel != null) {
return viewModel;
}
// if viewModel is null, create a new one
final T newViewModel = viewModelFactory.create<T>();
viewModelStore.put(newViewModel, key: key);
return newViewModel;
}