setFuture method

void setFuture([
  1. Future<T>? future
])

Invokes the stored futureCallback, or alternatively can accept a new Future object.

Implementation

void setFuture([Future<T>? future]) {
  clear();
  _future = future ??= futureCallback?.call();
  if (future == null) return;

  future.then<void>(
    (T data) {
      if (identical(_future, future)) {
        value = AsyncSnapshot<T>.withData(ConnectionState.done, data);
      }
    },
    onError: (Object error, StackTrace stackTrace) {
      if (identical(_future, future)) {
        value = AsyncSnapshot<T>.withError(ConnectionState.done, error, stackTrace);
      }
    },
  );

  // An implementation like `SynchronousFuture` may have already called the
  // .then() closure. Do not overwrite it in that case.
  if (connectionState != ConnectionState.done) {
    connectionState = ConnectionState.waiting;
  }
}