Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
43 lines (35 sloc) 1018 Bytes
import 'package:dio/dio.dart';
class CacheInterceptor extends Interceptor {
var _cache = new Map<Uri, Response>();
onRequest(RequestOptions options) {
Response response = _cache[options.uri];
if (options.extra["refresh"] == true) {
print("${options.uri}: force refresh, ignore cache! \n");
return options;
} else if (response != null) {
print("cache hit: ${options.uri} \n");
return response;
onResponse(Response response) {
_cache[response.request.uri] = response;
onError(DioError e) {
print('onError: $e');
main() async {
var dio = new Dio();
dio.options.baseUrl = "";
..add(LogInterceptor(requestHeader: false, responseHeader: false))
await dio.get("/"); // second request
await dio.get("/"); // Will hit cache
// Force refresh
await dio.get("/", options: Options(extra: {'refresh': true}));
You can’t perform that action at this time.