-
-
Save Vanethos/38504610d4dfb709d74caecea24dfae5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:dio/dio.dart'; | |
class CacheInterceptor extends Interceptor { | |
CacheInterceptor(); | |
var _cache = new Map<Uri, Response>(); | |
@override | |
onRequest(RequestOptions options) { | |
return options; | |
} | |
@override | |
onResponse(Response response) { | |
_cache[response.request.uri] = response; | |
} | |
@override | |
onError(DioError e) { | |
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment