useFuture<T> function

AsyncSnapshot<T> useFuture<T>(
  1. Future<T>? future, {
  2. T? initialData,
  3. bool preserveState = true,
})

The latest snapshot of future, from initialData on: waiting while the future runs, done with its value or error once it completes, which rebuilds the element, and none while there is no future. A snapshot keeps the latest data or error until the next.

The future is waited on from the first build that gives it on, the future of a later build instead once it is not the same; the result of a future left for another, or of one the element was unmounted with, is ignored. The snapshot of a future left for another is the last one's, not connected, with preserveState, or the first one, from initialData, without.

A config loaded once, its future kept by useMemoized so that a build does not start another:

HookBuilder(
  builder: (context) {
    final config = useFuture(useMemoized(loadConfig));
    return Text(config.hasData ? config.data! : 'Loading…');
  },
)

Implementation

AsyncSnapshot<T> useFuture<T>(
  Future<T>? future, {
  T? initialData,
  bool preserveState = true,
}) => use(_FutureHook(future, initialData, preserveState: preserveState));