diff --git a/dio/CHANGELOG.md b/dio/CHANGELOG.md index 9c54cf280..f954141c5 100644 --- a/dio/CHANGELOG.md +++ b/dio/CHANGELOG.md @@ -2,7 +2,9 @@ ## Unreleased -*None.* +- Revert changes to `CancelToken.cancel()` behavior, as a result the `DioError` + provided by the `CancelToken.cancelError` does not contain useful information + when the token was not used with a request. ## 5.1.0 diff --git a/dio/lib/src/cancel_token.dart b/dio/lib/src/cancel_token.dart index b4e430917..b049deb4c 100644 --- a/dio/lib/src/cancel_token.dart +++ b/dio/lib/src/cancel_token.dart @@ -31,13 +31,8 @@ class CancelToken { /// Cancel the request with the given [reason]. void cancel([Object? reason]) { - if (requestOptions == null) { - throw StateError( - 'CancelToken was canceled before being used in a request.', - ); - } _cancelError = DioError.requestCancelled( - requestOptions: requestOptions!, + requestOptions: requestOptions ?? RequestOptions(), reason: reason, stackTrace: StackTrace.current, ); diff --git a/dio/test/cancel_token_test.dart b/dio/test/cancel_token_test.dart index 0560945ee..bfbdcb5fc 100644 --- a/dio/test/cancel_token_test.dart +++ b/dio/test/cancel_token_test.dart @@ -16,10 +16,8 @@ void main() { token.cancel(reason); }); - test('cancel without use throws StateError', () async { - final token = CancelToken(); - - expect(() => token.cancel(), throwsStateError); + test('cancel without use does not throw (#1765)', () async { + CancelToken().cancel(); }); }); }