flutter_cache 0.0.6

  • Readme
  • Changelog
  • Example
  • Installing
  • 71

flutter_cache #

Dependencies Status file size GitHub Issues follows on twitter

A simple cache package for flutter. This package is a wrapper for shared preference and makes working with shared preference easier. Once it has been installed, you can do these things.

// create new cache.
Cache.remember('key', 'data'); 
Cache.write('key', 'data'); 

// add Cache lifetime on create
Cache.remember('key', 'data', 120); 
Cache.write('key', 'data', 120); 

// load Cache by key
Cache.load('key'); // This will load the cache data.

// destroy single cache by key
Cache.destroy('key');

// destroy all cache
Cache.clear();

Getting Started #

Installation #

First include the package dependency in your project's pubspec.yaml file

dependencies:
  flutter_cache: ^0.0.6

You can install the package via pub get:

flutter pub get

Then you can import it in your dart code like so

import 'package:flutter_cache/flutter_cache.dart';

What Can this Package Do ? #

  1. Cache String, Map, List<String> and List<Map> for forever or for a limited time.
  2. Load the cache you've cached.
  3. Clear All Cache.
  4. Clear Single Cache.

Usage #

Cache Fetch Data from API

If data already exist, then it will use the data in the cache. If it's not, It will fetch the data. You can also set Cache lifetime so your app would fetch again everytime the Cache dies.

import 'dart:convert';
import 'package:http/http.dart' as http;

var test = await Cache.remember('servers', () async {
  var response = await http.get('http://dummy.restapiexample.com/api/v1/employees');
  return jsonDecode(response.body)['data'];
}, 120); // cache for 2 mins

// or 

// cache for 2 mins
var test = await Cache.remember('servers', () async => jsonDecode( (await http.get( 'http://dummy.restapiexample.com/api/v1/employees' )).body )['data'], 120);

print(test);

Saved data for limited time

The data will be destroyed when it reached the time you set.

Cache.remember('key', 'data', 120); // saved for 2 mins or 120 seconds
Cache.write('key', 'data', 120);

Cache multipe datatype

You can cache multiple datatype. Supported datatype for now are String, Map, List<String> and List<Map>. When you use cache.load() to get back the data, it will return the data in the original datatype.

Cache.remember('key', { 
  'name' : 'Ashraf Kamarudin',
  'depth2' : {
    'name' : 'depth2',
    'depth3' : {
      'name': 'depth3'
    } 
  }
});

Cache.load('key'); // will return data in map datatype.

[0.0.1] - Test. #

  • Initial Release Version 0

[0.0.2] - Test. #

  • Added support for anonymous function
  • Update Readme with detailed Explaination

[0.0.3] - Fixes. #

  • fix readme, showed wrong example.

[0.0.4] - Added. #

  • added example on /example

[0.0.5] - Fixes. #

  • clear cache trace on cache destroy.
  • added test for cache destroy.

[0.0.6] - Fixes and Small Addition. #

  • await on anonymous function execute.
  • added ShouldCache type

example/example.dart

import 'package:flutter_cache/flutter_cache.dart';

void main() async {

  // create new cache.
  Cache.remember('key', 'data'); 
  Cache.write('key', 'data'); 

  // add Cache lifetime on create
  Cache.remember('key', 'data', 120); 
  Cache.write('key', 'data', 120); 

  // load Cache by key
  Cache.load('key'); // This will load the cache data.

  // destroy single cache by key
  Cache.destroy('key');

  // destroy all cache
  Cache.clear();

  await Cache.remember('key', () {
    return 'test'; // or logic fetching data from api;
  });

  // or 

  await Cache.remember('key', () => 'test');

  Cache.remember('key', 'data', 120); // saved for 2 mins or 120 seconds
  Cache.write('key', 'data', 120);

  // multi depth map datatype.
  Cache.remember('key', { 
    'name' : 'Ashraf Kamarudin',
    'depth2' : {
      'name' : 'depth2',
      'depth3' : {
        'name': 'depth3'
      } 
    }
  });

  Cache.load('key'); // will return data in map datatype.
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  flutter_cache: ^0.0.6

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:flutter_cache/flutter_cache.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
46
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
71
Learn more about scoring.

We analyzed this package on Jul 10, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.15
  • Flutter: 1.17.5

Analysis suggestions

Package does not support Flutter platform Android

Because:

  • package:flutter_cache/flutter_cache.dart that imports:
  • package:shared_preferences/shared_preferences.dart that imports:
  • package:shared_preferences_linux/shared_preferences_linux.dart that declares support for platforms: Linux

Package does not support Flutter platform Web

Because:

  • package:flutter_cache/flutter_cache.dart that imports:
  • package:shared_preferences/shared_preferences.dart that imports:
  • package:shared_preferences_linux/shared_preferences_linux.dart that declares support for platforms: Linux

Package does not support Flutter platform Windows

Because:

  • package:flutter_cache/flutter_cache.dart that imports:
  • package:shared_preferences/shared_preferences.dart that declares support for platforms: Android, iOS, Linux, macOS, Web

Package does not support Flutter platform iOS

Because:

  • package:flutter_cache/flutter_cache.dart that imports:
  • package:shared_preferences/shared_preferences.dart that imports:
  • package:shared_preferences_linux/shared_preferences_linux.dart that declares support for platforms: Linux

Package does not support Flutter platform macOS

Because:

  • package:flutter_cache/flutter_cache.dart that imports:
  • package:shared_preferences/shared_preferences.dart that imports:
  • package:shared_preferences_linux/shared_preferences_linux.dart that declares support for platforms: Linux

Package not compatible with SDK dart

Because:

  • flutter_cache that is a package requiring null.

Health issues and suggestions

Document public APIs. (-1 points)

33 out of 33 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Format lib/flutter_cache.dart.

Run flutter format to format lib/flutter_cache.dart.

Format lib/shouldCache.dart.

Run flutter format to format lib/shouldCache.dart.

Maintenance suggestions

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
flutter 0.0.0
shared_preferences ^0.5.6 0.5.8
Transitive dependencies
collection 1.14.12 1.14.13
file 5.2.1
flutter_web_plugins 0.0.0
intl 0.16.1
meta 1.1.8 1.2.2
path 1.7.0
path_provider_linux 0.0.1+2
path_provider_platform_interface 1.0.2
platform 2.2.1
plugin_platform_interface 1.0.2
process 3.0.13
shared_preferences_linux 0.0.2+1
shared_preferences_macos 0.0.1+10
shared_preferences_platform_interface 1.0.4
shared_preferences_web 0.1.2+7
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8 2.1.0-nullsafety
xdg_directories 0.1.0
Dev dependencies
flutter_test