flutter_lightweight_store 是一个易于使用的Flutter库,为您的Flutter应用程序提供了一种简单高效的持久化存储管理方式。与流行的 shared_preferences 库相比,它提供了多个优点,包括支持多实例、多文件存储,以及键值对的加密和解密。
介绍
flutter_lightweight_store 的一个突出特性是支持多实例和多文件存储,这是对 shared_preferences 的改进,后者只支持单一文件存储。
使用它,您可以创建多个存储实例,每个实例都有自己独立的存储文件。这使您能够以更结构化的方式组织数据,使其更易于管理和维护。
例如,您可以为用户设置创建一个实例,为应用配置创建另一个实例,为缓存数据创建另一个实例。每个实例都可以独立访问和操作,每个实例的数据都存储在自己的独立文件中。
用法
/// 💚The key-value data will save to a file named `com.your.name.xml/plist`(Android/iOS)
FlutterLightweightStoreModule sp = FlutterLightweightStoreModule("com.your.name");
/// 💚More instance ... one instance corresponds to one file.
FlutterLightweightStoreModule sp1 = FlutterLightweightStoreModule("com.file1");
FlutterLightweightStoreModule sp2 = FlutterLightweightStoreModule("com.file2");
FlutterLightweightStoreModule sp3 ..
.
/// 为 sp, sp1, sp2 ... 实例加上static, 以便项目中你可以方便访问, 当然你也可以随时创建使用和释放
/// setter
await sp.setString("string_key", "Hello world");
await sp.setInt("int_value", 10086);
await sp.setDouble("double_key", 10086.12306);
await sp.setBoolean("boolean_key", true);
/// getter
var str = await sp.getString("string_key");
var int = await sp.getInt("int_value");
var double = await sp.getDouble("double_key");
var boolean = await sp.getBoolean("boolean_key");
/// remove
await sp.removeKey('string_key');
/// contains
bool isContains = await sp.contains('string_key');
加密和解密
flutter_lightweight_store 的另一大特性是支持键值对的加密和解密。这使您能够安全地存储敏感数据,如用户凭证或个人信息,防止未经授权的访问。可配置仅加密 value 或 key-value 都加密。
static FlutterLightweightStoreModule secureSp =
FlutterLightweightStoreModule("com.secure.data", aesKey: "12345678abcdefABCDEF", aesIV: "11110000FFFFHHHH");
await secure.setString("key", "value"); # 加密后存储到文件com.secure.data.xml/plist中
var valueDecrypted = await secure.getString("key"); # 解密后返回给业务
Features
Please feel free to request new features at the issue tracker