sraph_cache 1.0.0
sraph_cache: ^1.0.0 copied to clipboard
Flutter package for reading and writing simple key-value pairs.
Sraph Cache #
Base on shared_preferences and add listenable cache.
Usage #
To use this plugin, add sraph_cache as a dependency in your pubspec.yaml file.
Examples #
Here are small examples that show you how to use the API.
Write data
// init Cache.
await Cache.ensureInitialized();
// Save an integer value to 'counter' key.
await Cache.set<int>('counter', 10);
// Save an boolean value to 'repeat' key.
await Cache.set<bool>('repeat', true);
// Save an double value to 'decimal' key.
await Cache.set<double>('decimal', 1.5);
// Save an String value to 'action' key.
await Cache.set<String>('action', 'Start');
// Save an list of strings to 'items' key.
await Cache.set<List<String>>('items', <String>['Earth', 'Moon', 'Sun']);
Read data
// Try reading data from the 'counter' key. If it doesn't exist, returns null.
final int? counter = Cache.get<int>('counter');
// Try reading data from the 'repeat' key. If it doesn't exist, returns null.
final bool? repeat = Cache.get<bool>('repeat');
// Try reading data from the 'decimal' key. If it doesn't exist, returns null.
final double? decimal = Cache.get<double>('decimal');
// Try reading data from the 'action' key. If it doesn't exist, returns null.
final String? action = Cache.get<String>('action');
// Try reading data from the 'items' key. If it doesn't exist, returns null.
final List<String>? items = Cache.get<List<String>>('items');
Remove an entry
// Remove data for the 'counter' key.
final success = await Cache.remove('counter');
Listen to changes
return ValueListenableBuilder(
valueListenable: Cache.listenable(),
builder: (context, _, child) {
...
},
);