use<R> function

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

The value of hook for the build running: the value of its state, which the first build to use a hook in this place creates; see HookState. Called by the build of a HookWidget, of the state of a StatefulHookWidget or of a HookBuilder, directly or by the functions it calls, such as useState.

Duration useElapsed() => use(const _ElapsedHook());

final class _ElapsedHook extends Hook<Duration> {
  const _ElapsedHook();

  @override
  _ElapsedState createState() => _ElapsedState();
}

final class _ElapsedState extends HookState<Duration, _ElapsedHook> {
  final _stopwatch = Stopwatch()..start();

  @override
  Duration build(BuildContext context) => _stopwatch.elapsed;
}

Implementation

R use<R>(Hook<R> hook) {
  final element = HookElement._current;
  if (element == null) {
    throw StateError(_outside(hook.debugLabel));
  }
  return element._use(hook);
}