useContext function

HookContext useContext()

Retrieves the current HookContext.

Shorthand for HookContext.current. This method can only be called during build in supported contexts (e.g. HookWidget or HookProviderContainer). Calling it outside of a valid context will throw an exception. After the first build, all subsequent builds must call this method in the same order, with Hooks of the same type.

Implementation

HookContext useContext() {
  final context = HookContext.current;
  assert(() {
    if (context == null) {
      throw FlutterError.fromParts([
        ErrorSummary("Tried to useContext() without an available HookContext"),
        ErrorDescription("useContext() can only be used during builds of valid HookContexts"),
        ErrorHint("To use hooks in Widgets, use HookWidget"),
      ]);
    }
    return true;
  }());
  return context!;
}