addFuture<T> method

VoidCallback addFuture<T>(
  1. Future<T> future, {
  2. DataCallback<T>? onData,
  3. ErrorCallback? onError,
})

Implementation

VoidCallback addFuture<T>(Future<T> future,
        {DataCallback<T>? onData, ErrorCallback? onError}) =>
    addSubscription((removeSubscription) {
      bool canceled = false;
      future.then(
          onData == null
              ? (value) {
                  if (canceled) return;
                  removeSubscription();
                }
              : (value) {
                  if (canceled) return;
                  removeSubscription();
                  onData(value);
                },
          onError: onError == null
              ? (error, stackTrace) {
                  if (canceled) return;
                  removeSubscription();
                  Error.throwWithStackTrace(error, stackTrace);
                }
              : (error, stackTrace) {
                  removeSubscription();
                  return onError(error, stackTrace);
                });
      return () => canceled = true;
    });