futureAtom<A> function

FutureAtom<A> futureAtom<A>(
  1. AtomReader<Future<A>> create
)

Create an AtomWithParent that returns a FutureValue representing the current state of the Future's execution.

The parent property is set to the Future itself, so you can await it if required.

Implementation

FutureAtom<A> futureAtom<A>(
  AtomReader<Future<A>> create,
) =>
    AtomWithParent(atom(create), (get, future) {
      bool disposed = false;
      get.onDispose(() => disposed = true);

      get(future).then((value) {
        if (disposed) return;
        get.setSelf(FutureValue.data(value));
      }, onError: (err, stack) {
        if (disposed) return;
        get.setSelf(FutureValue.error(err, stack));
      });

      return FutureValue.loading(get.self()?.dataOrNull);
    });