getCancellationReason static method

String? getCancellationReason(
  1. Object error
)

Extract the cancellation reason/message from an error

Returns the reason string if the error is a cancellation, otherwise returns null.

Example:

catch (e) {
  final reason = CancellationHelper.getCancellationReason(e);
  if (reason != null) {
    print('Cancelled: $reason');
  }
}

Implementation

static String? getCancellationReason(Object error) {
  if (!isCancelled(error)) return null;

  if (error is CancelledError) {
    return error.message;
  }

  if (error is DioException) {
    return error.message;
  }

  return null;
}