of<N extends StateNotifier> static method

N of<N extends StateNotifier>(
  1. BuildContext context, {
  2. bool listen = false,
})

Obtains the nearest StateNotifier up its widget tree.

The build context is rebuilt when StateNotifier is changed if listen set to true.

Implementation

static N of<N extends StateNotifier>(
  BuildContext context, {
  bool listen = false,
}) {
  final provider = listen
      ? context.dependOnInheritedWidgetOfExactType<Provider<N>>()
      : context.getElementForInheritedWidgetOfExactType<Provider<N>>()?.widget
          as Provider<N>?;
  assert(provider != null, 'No Provider<${N.runtimeType}> found in context.');
  return provider!.stateNotifier;
}