@@ -14,6 +14,7 @@ import 'util/response_extension.dart';
1414/// Cache interceptor
1515class DioCacheInterceptor extends Interceptor {
1616 static const String _getMethodName = 'GET' ;
17+ static const String _postMethodName = 'POST' ;
1718
1819 final CacheOptions _options;
1920 final CacheStore _store;
@@ -28,13 +29,13 @@ class DioCacheInterceptor extends Interceptor {
2829 RequestOptions request,
2930 RequestInterceptorHandler handler,
3031 ) async {
31- if (_shouldSkipRequest (request)) {
32+ final options = _getCacheOptions (request);
33+
34+ if (_shouldSkipRequest (request, options: options)) {
3235 handler.next (request);
3336 return ;
3437 }
3538
36- final options = _getCacheOptions (request);
37-
3839 if (options.policy != CachePolicy .refresh &&
3940 options.policy != CachePolicy .refreshForceCache) {
4041 final cacheResp = await _getCacheResponse (request);
@@ -57,13 +58,14 @@ class DioCacheInterceptor extends Interceptor {
5758 Response response,
5859 ResponseInterceptorHandler handler,
5960 ) async {
60- if (_shouldSkipRequest (response.requestOptions) ||
61+ final options = _getCacheOptions (response.requestOptions);
62+
63+ if (_shouldSkipRequest (response.requestOptions, options: options) ||
6164 response.statusCode != 200 ) {
6265 handler.next (response);
6366 return ;
6467 }
6568
66- final options = _getCacheOptions (response.requestOptions);
6769 final policy = options.policy;
6870
6971 if (policy == CachePolicy .noCache) {
@@ -93,7 +95,9 @@ class DioCacheInterceptor extends Interceptor {
9395 DioError err,
9496 ErrorInterceptorHandler handler,
9597 ) async {
96- if (_shouldSkipRequest (err.requestOptions, error: err)) {
98+ final options = _getCacheOptions (err.requestOptions);
99+
100+ if (_shouldSkipRequest (err.requestOptions, options: options, error: err)) {
97101 handler.next (err);
98102 return ;
99103 }
@@ -105,7 +109,6 @@ class DioCacheInterceptor extends Interceptor {
105109 returnResponse = true ;
106110 } else {
107111 // Check if we can return cache
108- final options = _getCacheOptions (err.requestOptions);
109112 final hcoeExcept = options.hitCacheOnErrorExcept;
110113
111114 if (hcoeExcept != null ) {
@@ -184,9 +187,19 @@ class DioCacheInterceptor extends Interceptor {
184187 return options.store ?? _store;
185188 }
186189
187- bool _shouldSkipRequest (RequestOptions ? request, {DioError ? error}) {
188- var result = error? .type == DioErrorType .cancel;
189- result | = (request? .method.toUpperCase () != _getMethodName);
190+ bool _shouldSkipRequest (
191+ RequestOptions ? request, {
192+ required CacheOptions options,
193+ DioError ? error,
194+ }) {
195+ if (error? .type == DioErrorType .cancel) {
196+ return true ;
197+ }
198+
199+ final rqMethod = request? .method.toUpperCase ();
200+ var result = (rqMethod != _getMethodName);
201+ result & = (! options.allowPostMethod || rqMethod != _postMethodName);
202+
190203 return result;
191204 }
192205
0 commit comments