get method

  1. @override
Future<T> get (
  1. {bool forceReload: false,
  2. bool allowCacheFallback: true,
  3. bool skipCacheWrite: false}
)
override

Retrieve the data from RAM if possible, otherwise fallback to cache or network, depending on the strategy.

If forceReload is true then this will fetch from the network, using the cache as a fallback unless allowCacheFallback is false.

allowCacheFallback only affects network requests. The cache can still be used if it is not expired and forceReload is false.

Implementation

@override
Future<T> get(
    {bool forceReload: false,
    bool allowCacheFallback: true,
    bool skipCacheWrite: false}) async {
  if (cache.data != null && !forceReload) {
    // print('${cache.basename}: Using previously loaded value.');
    return cache.data;
  } else if (forceReload ||
      strategy == CacheStrategy.networkFirst ||
      await isExpired) {
    print('${cache.basename}: Fetching from $url');
    final contents = await _tryFetchContents();
    if (contents != null) {
      print('$url Fetched.');
      if (!skipCacheWrite) {
        print('Updating cache...');
        return cache.write(contents);
      } else {
        return cache._update(contents);
      }
    } else {
      if (allowCacheFallback) {
        print('$url Using a cached copy if available.');
        return cache.get();
      } else {
        print('Not attempting to find in cache.');
        return null;
      }
    }
  } else {
    print('Loading cached copy of ${cache.basename}');
    return cache.get();
  }
}