Flutter Cache JSON response using http response header. You can you create your own cache with Interceptors on top of Dio requests.
You can create in on your own:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import 'package:dio/dio.dart'; class CacheInterceptor extends Interceptor { CacheInterceptor(); var _cache = new Map<Uri, Response>(); @override onRequest(RequestOptions options) async { return options; } @override onResponse(Response response) async { _cache[response.request.uri] = response; } @override onError(DioError e) async{ print('onError: $e'); if (e.type == DioErrorType.CONNECT_TIMEOUT || e.type == DioErrorType.DEFAULT) { var cachedResponse = _cache[e.request.uri]; if (cachedResponse != null) { return cachedResponse; } } return e; } } |
and then use it with:
1 |
final dio = Dio()..interceptors.add(CacheInterceptor()); |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.