Package selection guidance compiled from public package information, community data, and project usage.
flutter_cache is a lightweight package that simplifies working with shared preferences in Flutter applications. It acts as a wrapper around shared preferences, providing an easy-to-use API for storing and retrieving data persistently across app restarts. Designed for simplicity and efficiency, it supports all major platforms including Android, iOS, Web, macOS, Windows, and Linux. The package abstracts common caching operations, making it ideal for managing small amounts of persistent data like user settings, tokens, or cached responses.
Use cases
Storing user preferences
Caching API response data
Persisting login tokens
Saving app state
Managing configuration settings
Key features
Simple API
Shared preferences wrapper
Cross-platform support
Easy data serialization
Lightweight and fast
Suitable for
Beginner Flutter developers
Apps needing basic persistence
Small-scale data storage
Rapid prototyping
Simple caching needs
Considerations
Not suitable for large datasets
Limited to key-value storage
No built-in expiration mechanism
Relies on shared preferences performance
Does not support complex queries
Original README
Original README snapshot from the linked source repository. Visit the repository for the latest content.
flutter_cache
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.
import'package:flutter_cache/flutter_cache.dart'as cache;
// 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.// return `defaultValue` if key not exists
cache.load('key', 'defaultValue')
// 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
Cache String, Map, List<String> and List<Map> for forever or for a limited time.
Load the cache you've cached.
Clear All Cache.
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;
import'package:flutter_cache/flutter_cache.dart'as cache;
var employees = await cache.remember('employees', () async {
var response = await http.get('http://dummy.restapiexample.com/api/v1/employees');
returnjsonDecode(response.body)['data'].cast<Map>();
}, 120); // cache for 2 mins// or // cache for 2 minsvar employees = await cache.remember(
'servers',
() async => jsonDecode(
(await http.get( 'http://dummy.restapiexample.com/api/v1/employees' )
).body )['data'].cast<Map>()
, 120);
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.