of<T extends ScopeController<Bloc<BlocEvent, BlocState>>> static method

T of<T extends ScopeController<Bloc<BlocEvent, BlocState>>>(
  1. BuildContext context, {
  2. bool listen = true,
})

Static helper method that retrieves the ScopeController of type T from the widget tree. It uses the InheritedWidget pattern to locate the nearest _ScopeInherited widget, which contains the controller.

If the controller is not found in the widget tree, it throws a FlutterError.

Implementation

static T of<T extends ScopeController>(BuildContext context,
    {bool listen = true}) {
  /// Retrieve the _ScopeInherited widget containing the controller.
  final _ScopeInherited<T> inherited =
      context.inhOf<_ScopeInherited<T>>(listen: listen);

  /// Return the controller casted to the expected type.
  return inherited.controller as T;
}