handleFuture method

  1. @preferInline
  2. @internal
void handleFuture(
  1. FutureOr<T> create(), {
  2. required bool didChangeDependency,
})
inherited

Listens to a Future and convert it into an AsyncValue.

Implementation

@preferInline
@internal
void handleFuture(
  FutureOr<T> Function() create, {
  required bool didChangeDependency,
}) {
  _handleAsync(didChangeDependency: didChangeDependency, ({
    required data,
    required done,
    required error,
    required last,
  }) {
    final futureOr = create();
    if (futureOr is! Future<T>) {
      data(futureOr);
      done();
      return null;
    }
    // Received a Future<T>

    var running = true;
    void cancel() {
      running = false;
    }

    futureOr.then(
      (value) {
        if (!running) return;
        data(value);
        done();
      },
      // ignore: avoid_types_on_closure_parameters
      onError: (Object err, StackTrace stackTrace) {
        if (!running) return;
        error(err, stackTrace);
        done();
      },
    );

    last(futureOr, cancel);

    return cancel;
  });
}