项目作者: SekolahCode

项目描述 :
Shared Preference made Simple.
高级语言: Dart
项目地址: git://github.com/SekolahCode/flutter_cache.git
创建时间: 2020-05-21T16:04:58Z
项目社区:https://github.com/SekolahCode/flutter_cache

开源协议:MIT License

下载


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.

  1. import 'package:flutter_cache/flutter_cache.dart' as cache;
  2. // create new cache.
  3. cache.remember('key', 'data');
  4. cache.write('key', 'data');
  5. // add Cache lifetime on create
  6. cache.remember('key', 'data', 120);
  7. cache.write('key', 'data', 120);
  8. // load Cache by key
  9. cache.load('key'); // This will load the cache data.
  10. // return `defaultValue` if key not exists
  11. cache.load('key', 'defaultValue')
  12. // destroy single cache by key
  13. cache.destroy('key');
  14. // destroy all cache
  15. cache.clear();

Getting Started

Installation

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

  1. dependencies:
  2. flutter_cache: ^0.1.0

You can install the package via pub get:

  1. flutter pub get

Then you can import it in your dart code like so

  1. import 'package:flutter_cache/flutter_cache.dart' as cache;

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.

  1. import 'dart:convert';
  2. import 'package:http/http.dart' as http;
  3. import 'package:flutter_cache/flutter_cache.dart' as cache;
  4. var employees = await cache.remember('employees', () async {
  5. var response = await http.get('http://dummy.restapiexample.com/api/v1/employees');
  6. return jsonDecode(response.body)['data'].cast<Map>();
  7. }, 120); // cache for 2 mins
  8. // or
  9. // cache for 2 mins
  10. var employees = await cache.remember(
  11. 'servers',
  12. () async => jsonDecode(
  13. (await http.get( 'http://dummy.restapiexample.com/api/v1/employees' )
  14. ).body )['data'].cast<Map>()
  15. , 120);

Saved data for limited time

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

  1. cache.remember('key', 'data', 120); // saved for 2 mins or 120 seconds
  2. 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.

  1. cache.remember('key', {
  2. 'name' : 'Ashraf Kamarudin',
  3. 'depth2' : {
  4. 'name' : 'depth2',
  5. 'depth3' : {
  6. 'name': 'depth3'
  7. }
  8. }
  9. });
  10. cache.load('key'); // will return data in map datatype.