ignoreCancellation function

Future<void> ignoreCancellation(
  1. FutureOr operation(
      ),
    1. {FutureOr<void> onError(
      1. Object e,
      2. StackTrace stackTrace
      )?,
    2. FutureOr<void> whenComplete(
        )?,
      1. FutureOr<void> whenCompleteOrCancelled(
          )?}
        )

        Runs an operation and silently catches cancellations. Other exceptions can be handled using onError. If onError is null, exceptions will be rethrown.

        If a custom exception is passed when cancelling the CancellationToken, it won't be handled by this function.

        Inside a try/catch

        try {
          ignoreCancellation(() async {
            response = await apiEndpoint().asCancellable(cancellationToken);
          });
        } catch (e, stackTrace) {
          // Other errors will be rethrown
        }
        

        Outside of a try/catch

        ignoreCancellation(
          () async {
            response = await apiEndpoint().asCancellable(cancellationToken);
          },
          catchError: (e, stackTrace) {
            // This is the equivalent of a catch block
          },
          whenComplete: () {
            // This is similar to a finally block, but is only called if the
            // operation wasn't cancelled
          },
          whenCompleteOrCancelled: () {
            // This is the equivalent of a finally block
          },
        );
        

        Implementation

        Future<void> ignoreCancellation(
          FutureOr<dynamic> Function() operation, {
          FutureOr<void> Function(Object e, StackTrace stackTrace)? onError,
          FutureOr<void> Function()? whenComplete,
          FutureOr<void> Function()? whenCompleteOrCancelled,
        }) async {
          try {
            await operation();
            await whenComplete?.call();
          } on CancelledException {
            // Ignore cancellation
          } catch (e, stackTrace) {
            if (onError == null) rethrow;
            await onError(e, stackTrace);
            await whenComplete?.call();
          } finally {
            await whenCompleteOrCancelled?.call();
          }
        }