diff --git a/lib/src/manager_dio.dart b/lib/src/manager_dio.dart index 3645f86..8a91f9a 100644 --- a/lib/src/manager_dio.dart +++ b/lib/src/manager_dio.dart @@ -5,6 +5,7 @@ import 'package:dio/dio.dart'; import 'package:dio_http_cache/src/core/config.dart'; import 'package:dio_http_cache/src/core/manager.dart'; import 'package:dio_http_cache/src/core/obj.dart'; +import 'package:flutter/foundation.dart'; const DIO_CACHE_KEY_TRY_CACHE = "dio_cache_try_cache"; const DIO_CACHE_KEY_MAX_AGE = "dio_cache_max_age"; @@ -47,14 +48,17 @@ class DioCacheManager { } var responseDataFromCache = await _pullFromCacheBeforeMaxAge(options); if (null != responseDataFromCache) { - return handler.resolve(_buildResponse( - responseDataFromCache, responseDataFromCache.statusCode, options), true); + return handler.resolve( + await buildResponse( + responseDataFromCache, responseDataFromCache.statusCode, options), + true); } return handler.next(options); } _onResponse(Response response, ResponseInterceptorHandler handler) async { - if ((response.requestOptions.extra[DIO_CACHE_KEY_TRY_CACHE] ?? false) == true && + if ((response.requestOptions.extra[DIO_CACHE_KEY_TRY_CACHE] ?? false) == + true && response.statusCode != null && response.statusCode! >= 200 && response.statusCode! < 300) { @@ -65,9 +69,10 @@ class DioCacheManager { _onError(DioError e, ErrorInterceptorHandler handler) async { if ((e.requestOptions.extra[DIO_CACHE_KEY_TRY_CACHE] ?? false) == true) { - var responseDataFromCache = await _pullFromCacheBeforeMaxStale(e.requestOptions); + var responseDataFromCache = + await _pullFromCacheBeforeMaxStale(e.requestOptions); if (null != responseDataFromCache) { - var response = _buildResponse(responseDataFromCache, + var response = await buildResponse(responseDataFromCache, responseDataFromCache.statusCode, e.requestOptions); return handler.resolve(response); @@ -76,8 +81,9 @@ class DioCacheManager { return handler.next(e); } - Response _buildResponse( - CacheObj obj, int? statusCode, RequestOptions options) { + @protected + Future buildResponse( + CacheObj obj, int? statusCode, RequestOptions options) async { Headers? headers; if (null != obj.headers) { headers = Headers.fromMap((Map>.from( @@ -97,7 +103,8 @@ class DioCacheManager { return Response( data: data, headers: headers, - requestOptions: options.copyWith(extra: options.extra..remove(DIO_CACHE_KEY_TRY_CACHE)), + requestOptions: options.copyWith( + extra: options.extra..remove(DIO_CACHE_KEY_TRY_CACHE)), statusCode: statusCode ?? 200); } @@ -147,8 +154,10 @@ class DioCacheManager { // try to get maxAge and maxStale from cacheControl Map parameters; try { - parameters = HeaderValue.parse("${HttpHeaders.cacheControlHeader}: $cacheControl", - parameterSeparator: ",", valueSeparator: "=") + parameters = HeaderValue.parse( + "${HttpHeaders.cacheControlHeader}: $cacheControl", + parameterSeparator: ",", + valueSeparator: "=") .parameters; _maxAge = _tryGetDurationFromMap(parameters, "s-maxage"); if (null == _maxAge) { @@ -179,7 +188,8 @@ class DioCacheManager { callback(_maxAge, maxStale); } - Duration? _tryGetDurationFromMap(Map parameters, String key) { + Duration? _tryGetDurationFromMap( + Map parameters, String key) { if (parameters.containsKey(key)) { var value = int.tryParse(parameters[key]!); if (null != value && value >= 0) { diff --git a/pubspec.yaml b/pubspec.yaml index 9fa5126..77f8eb7 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -7,14 +7,14 @@ authors: homepage: https://github.com/hurshi/dio-http-cache environment: - sdk: '>=2.12.0 <3.0.0' + sdk: '>=2.13.0 <3.0.0' dependencies: crypto: ^3.0.0 dio: ^4.0.0 flutter: sdk: flutter - json_serializable: ^4.0.3 + json_serializable: ^6.0.0 path: ^1.8.0 quiver: ^3.0.0 sqflite: ^2.0.0+3