useMemoizedFuture<T> function

MemoizedAsyncSnapshot<T> useMemoizedFuture<T>(
  1. Future<T> future(), {
  2. List keys = const <dynamic>[],
  3. T? initialData,
  4. bool preserveState = true,
})

Subscribes to a Future and returns its current state in a MemoizedAsyncSnapshot. The future is memoized and will only be re-called if any of the keys change or if MemoizedAsyncSnapshot.refresh is called.

  • initialData specifies what initial value the AsyncSnapshot should have.
  • preserveState defines if the current value should be preserved when changing the Future instance.

See also:

  • useFuture, the hook responsible for getting the future.
  • useMemoized, the hook responsible for the memoization.

Implementation

MemoizedAsyncSnapshot<T> useMemoizedFuture<T>(
    Future<T> Function() future, {
      List<dynamic> keys = const <dynamic>[],
      T? initialData,
      bool preserveState = true,
    }) {
  final refresh = useState(0);
  final result = useFuture(
    useMemoized(future, [refresh.value, ...keys]),
    initialData: initialData,
    preserveState: preserveState,
  );

  void refreshMe() => refresh.value++;

  return MemoizedAsyncSnapshot<T>(result, refreshMe);
}