useFuture<T> function
Subscribes to a Future and returns its current state as an AsyncSnapshot.
preserveState
determines if the current value should be preserved when changing the Future instance.
The Future needs to be created outside of useFuture. If the Future is created inside useFuture, then, every time the build method gets called, the Future will be called again. One way to create the Future outside of useFuture is by using useMemoized.
// BAD
useFuture(fetchFromDatabase());
// GOOD
final result = useMemoized(() => fetchFromDatabase());
useFuture(result);
See also:
Implementation
AsyncSnapshot<T> useFuture<T>(
Future<T>? future, {
T? initialData,
bool preserveState = true,
}) {
return use(
_FutureHook(
future,
initialData: initialData,
preserveState: preserveState,
),
);
}