useState<T> function Null safety

ValueNotifier<T> useState<T>(
  1. T initialData
)

Creates a variable and subscribes to it.

Whenever ValueNotifier.value updates, it will mark the caller HookWidget as needing a build. On the first call, it initializes ValueNotifier to initialData. initialData is ignored on subsequent calls.

The following example showcases a basic counter application:

class Counter extends HookWidget {
  @override
  Widget build(BuildContext context) {
    final counter = useState(0);

    return GestureDetector(
      // automatically triggers a rebuild of the Counter widget
      onTap: () => counter.value++,
      child: Text(counter.value.toString()),
    );
  }
}

See also:

Implementation

ValueNotifier<T> useState<T>(T initialData) {
  return use(_StateHook(initialData: initialData));
}