find<T extends ViewModel> static method

T find<T extends ViewModel>(
  1. BuildContext context, {
  2. bool listen = false,
})

Implementation

static T find<T extends ViewModel>(
  BuildContext context, {
  bool listen = false,
}) {
  ViewModelProviderInheritedWidget<T>? provider;
  if (listen) {
    provider = context.dependOnInheritedWidgetOfExactType<
        ViewModelProviderInheritedWidget<T>>();
  } else {
    provider = context
        .findAncestorWidgetOfExactType<ViewModelProviderInheritedWidget<T>>();
  }
  if (provider == null) {
    throw 'No ViewModelProvider found for $T';
  }
  return provider.viewModel;
}