getViewModel<T extends ViewModel> function

T getViewModel <T extends ViewModel>(ViewModelStoreOwner owner, ViewModelProvider<T> provider)

获取ViewModel实例

通过给定的ownerprovider来获取T实例, 如果owner中已有T类型的实例,则会直接返回该实例, 如果owner中不存在T类型的实例,则会先使用provider创建T实例并保存。

Implementation

T getViewModel<T extends ViewModel>(
    ViewModelStoreOwner owner, ViewModelProvider<T> provider) {
  var viewModel = owner.viewModelStore._get(provider.runtimeType);

  if (viewModel is T) {
    return viewModel;
  }

  viewModel = provider.createViewModel();

  owner.viewModelStore._put(provider.runtimeType, viewModel);

  return viewModel;
}