whenCancel method

void whenCancel(
  1. void callback(
    1. String?
    )
)

Adds a cancellation callback.

添加取消回调。

The callback will be invoked when the request is cancelled. If the request is already cancelled, the callback is invoked immediately.

当请求被取消时,会调用此回调。 如果已经取消,立即调用回调。

Parameters / 参数:

  • callback: Function to call when cancelled / 取消时调用的函数

Example / 示例:

cancelToken.whenCancel((reason) {
  print('Request cancelled: $reason');
});

Implementation

void whenCancel(void Function(String?) callback) {
  if (_isCancelled) {
    callback(_cancelReason);
  } else {
    _callbacks.add(callback);
  }
}