local_cache_sync
A very simple and easy-to-use Flutter
local repository, suitable for storing a list of lightweight data locally (such as device information stored locally by the user, or caching a series of user information).
local_cache_sync
All methods are synchronous , not asynchronous . This means that you don’t need to use await
it to get the data. Among them flutter
, this can significantly reduce StatefulWidget
the number and greatly reduce the complexity of the code.
Start
pubspec.yaml
path_provider: ^1.4.5 local_cache_sync: ^1.1.0
Set Cache Path.
After 1.2.0:
void main() async { WidgetsFlutterBinding.ensureInitialized(); LocalCacheSync.instance.setCachePath( await getTemporaryDirectory(), 'example_app/', ); runApp(MyApp()); }
Before(less than or equal to 1.1.1):
getTemporaryDirectory().then((uri) { LocalCacheSync.instance.setCachePath(uri.path); });
User Default Demo
Switch
The value of the component will be cached locally, and will be retained even after restarting the App
The use of local_cache_sync
save and read parameters are synchronized, which means that the assignment is to save, and in StatelessWidget
, you can immediately use the data.
Switch( value: LocalCacheSync.userDefault.getWithKey<bool>('switch-A'), onChanged: (v) { setState(() { LocalCacheSync.userDefault.setWithKey<bool>('switch-A', v); }); }, ),
Usage: User Default Cache
Using the local_cache_sync
implementation to save user-defined settings is very simple, only need to assign and take values, without asynchronous waiting , you can save the parameters to the local.
Reading parameters is also synchronous, which means you can StatelessWidget
immediately use the data in.
Use Function:
Save values
LocalCacheSync.userDefault.setWithKey<bool>('isDarkMode',true); LocalCacheSync.userDefault.setWithKey<String>('token','aabbccdd'); LocalCacheSync.userDefault.setWithKey<Map>('x-config',{'id':1243});
Read values
var res = LocalCacheSync.userDefault.getWithKey<bool>('isDarkMode'); var res = LocalCacheSync.userDefault.getWithKey<String>('token'); var res = LocalCacheSync.userDefault.getWithKey<Map>('x-config');
Use operator:
Save values
LocalCacheSync.userDefault['isDarkMode'] = true; LocalCacheSync.userDefault['token'] = 'aabbccdd'; LocalCacheSync.userDefault['x-config'] = {'id':1243};
Read values
bool res = LocalCacheSync.userDefault['isDarkMode']; String res = LocalCacheSync.userDefault['token']; Map res = LocalCacheSync.userDefault['x-config'];
DefaultValueCache
Eazy used with static property.
// Creat class: class ProjectCustomUserDefault { static var autoLogin = const DefaultValueCache<bool>('autoLogin', false); } // save ProjectCustomUserDefault.autoLogin.value = true; // read bool isAutoLogin = ProjectCustomUserDefault.autoLogin.value
Usage: Table Cache list management
If you need to manage a series of values, please use LocalCacheLoader
, only need a channel
flag, you can manage a series of values.
Lazy Load
LocalCacheLoader
The effect of lazy loading is realized internally: only value
when the attribute is fetched , the data is really loaded.
In the application, add that you have devices 1-100 displayed in Listview.builder. Only when device 100 is about to enter the screen will the cache parameters of device 100 be actually loaded. In other words, LocalCacheLoader will not cause long list stalls.
Model Example
I recommend that you create your model like this:
Create class load from loader.
class Device { final String uuid; final String name; final int type; Device({ this.uuid, this.name, this.type, }); Device.formJson(Map<String, dynamic> map) : this( uuid: map['uuid'], name: map['name'], type: map['type'], ); static LocalCacheLoader get _loader => LocalCacheLoader('device'); static List<Device> all() { return _loader.all .map<Device>( (cache) => Device.fromJson(cache), ) .toList(); } LocalCacheObject save() { return Device._loader.saveById(uuid, jsonMap); } Map<String, dynamic> get jsonMap => { 'uuid': uuid, 'name': name, 'type': type, }; }
You can also package a loader to read and write other information. For lightweight storage, the above is very simple and easy to use.
caveat
Do not use local_cache_sync in io-intensive scenarios, for example, to store the scan results 10 times per second in real time.
Although blocking the main thread in flutter will not cause UI stalls, you should still not use it in io-intensive scenarios, which is beyond the scope of the local_cache_sync design.
Download local cache sync source code on GitHub
https://github.com/mjl0602/local_cache_sync
Provides the list of the opensource Flutter apps collection with GitHub repository.