use<T> function

T use<T>(
  1. Hook<T> hook
)

Register hook in the current HookContext and return its value.

Shorthand for HookContext.use on 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

T use<T>(Hook<T> hook) {
  assert(() {
    if (HookContext.current == null) {
      throw FlutterError.fromParts([
        ErrorSummary("Tried to use() a hook without an available HookContext"),
        ErrorDescription("Hooks can only be used during builds of valid HookContexts"),
        ErrorHint("To use hooks in Widgets, use HookWidget"),
        DiagnosticableNode(name: "hook", value: hook, style: null),
      ]);
    }
    return true;
  }());
  return HookContext.current!.use(hook);
}