onResponse method

  1. @override
void onResponse(
  1. Response response,
  2. ResponseInterceptorHandler handler
)
override

Called when the response is about to be resolved.

Implementation

@override
void onResponse(
  Response response,
  ResponseInterceptorHandler handler,
) async {
  final cacheOptions = _getCacheOptions(response.requestOptions);

  if (_shouldSkip(
    response.requestOptions,
    response: response,
    options: cacheOptions,
  )) {
    handler.next(response);
    return;
  }

  if (cacheOptions.policy == CachePolicy.noCache) {
    // Delete previous potential cached response
    await _getCacheStore(cacheOptions).delete(
      cacheOptions.keyBuilder(response.requestOptions),
    );
  }

  // Is status 304 being set as valid status?
  if (response.statusCode == 304) {
    // Update cache response with response header values
    final cacheResponse = await _loadResponse(response.requestOptions);
    if (cacheResponse != null) {
      response = cacheResponse..updateCacheHeaders(response);
    }
  }

  await _saveResponse(
    response,
    cacheOptions,
    statusCode: response.statusCode,
  );

  handler.next(response);
}