whenCancelled abstract method

void Function() whenCancelled(
  1. void callback(
    1. Cancelled
    )
)

Registers callback for cancellation and returns an unregister function.

Calls it once, synchronously, with the Cancelled when the job accepts a cancellation, without waiting for the body; a refused cancellation does not call it, and a job that ends Done or Failed releases it uncalled. Registering does not observe a Failed outcome. The callback is synchronous: its error goes to JobObserver.onError and on to the answer, and the future of an async one is not awaited, nor are its errors caught here.

final unregister = job.whenCancelled((cancelled) {
  print(cancelled.reason);
});
// When the listener is no longer needed:
unregister();

See Listening for the cancellation on the outcomes page for when the callback runs and where its errors go.

Implementation

void Function() whenCancelled(void Function(Cancelled) callback);