jt_preferences
Preferences 可无缝集成到您的项目中,提升用户体验和应用功能。通过 JT Preferences 简化偏好设置管理并优化您的应用。
- 星标
- 5
- 复刻
- 0
- 最近推送(UTC)
- 2025年2月21日
- 项目状态
- 已归档
原始 README
以下为英文项目原文快照,最新内容请访问 GitHub。
JtPreferences
Supported data types are int, double, bool, String and Writable object.
Usage
To use this plugin, add jt_preferences as
a dependency in your pubspec.yaml file
.
Examples
Here are small examples that show you how to use the package.
Initialize
void main(List<String> args) async {
//for example (data/data/com.example.application/) without absolute path
await JtPreferences.initialize("path/path", encryptionKey: '16 length encryption key');
//Data will be encrypted if encryptionKey is not null
}
Write data
// Obtain shared preferences.
final preferences = await JtPreferences.getInstance();
// Save an integer value to 'counter' key.
await preferences.setInt('counter', 10);
// Save an boolean value to 'repeat' key.
await preferences.setBool('repeat', true);
// Save an double value to 'decimal' key.
await preferences.setDouble('decimal', 1.5);
// Save an String value to 'action' key.
await preferences.setString('action', 'Start');
//Save writable object
await preferences.saveObject(User(name: 'user', age: 12));
Example User Writable object
class User extends Writable {
final String name;
final int age;
@override
factory User.fromJson(Map<String, dynamic> map) {
return User(name: map['name'], age: map['age']);
}
@override
OnConflictStrategy? get onConflictStrategy => OnConflictStrategy.update;
@override
Map<String, dynamic> toJson() {
return {
"name": name,
"age": age,
};
}
User({
required this.name,
required this.age,
});
@override
String key => name;
}
Read data
// Try reading data from the 'counter' key. If it doesn't exist, returns null.
final int? counter = preferences.getInt('counter');
// Try reading data from the 'repeat' key. If it doesn't exist, returns null.
final bool? repeat = preferences.getBool('repeat');
// Try reading data from the 'decimal' key. If it doesn't exist, returns null.
final double? decimal = preferences.getDouble('decimal');
// Try reading data from the 'action' key. If it doesn't exist, returns null.
final String? action = preferences.getString('action');
final List<String>? keys = preferences.getKeys();
final object = preferences.getObject('user', (map) => User.fromJson(map));
print(object?.name);
print(object?.age);
//Reload data from storage
_preferences.reload();
Listen changes
//listen all changes
preferences.listen().listen((event) {
print("key $event updated");
});
//listen only specific key
preferences.stream(key: 'counter').listen((event) {
print("key $event updated");
});
Remove an entry
// Remove data for the 'counter' key.
final success = await preferences.remove('counter');
语言DartC++CMakeHTMLSwiftCKotlinObjective-C
分类
使用的依赖
如何选择适合项目的代码仓?
先按使用场景筛选,再比较项目活跃情况,最后到原始仓库核对使用要求。
- 通过仓库名、作者或描述搜索,再按技术分类缩小范围。
- 结合星标、复刻与最近推送比较项目;星标反映社区关注,不代表质量保证。
- 使用前在 GitHub 核对 README、许可证与 Issues。
来源与维护
“A repository is the most basic element of GitHub.”
GitHub Docs — About repositories ↗