This commit is contained in:
Developer
2026-03-31 03:48:14 +08:00
parent 62729615b7
commit 888363785b
26 changed files with 219 additions and 808 deletions

View File

@@ -0,0 +1,195 @@
/// 时间: 2025-03-25
/// 功能: SharedPreferences存储控制器
/// 介绍: 使用SharedPreferences实现本地存储功能
/// 最新变化: 重命名为SharedPreferencesStorageController
import 'package:shared_preferences/shared_preferences.dart';
/// SharedPreferences存储控制器类
/// 负责管理本地键值对存储基于SharedPreferences实现
class SharedPreferencesStorageController {
static SharedPreferences? _prefs;
/// 初始化SharedPreferences
static Future<void> init() async {
try {
_prefs = await SharedPreferences.getInstance();
} catch (e) {
throw e;
}
}
/// 获取SharedPreferences实例
static Future<SharedPreferences> _getInstance() async {
if (_prefs == null) {
await init();
}
return _prefs!;
}
/// 存储字符串值
static Future<void> setString(String key, String value) async {
try {
final instance = await _getInstance();
await instance.setString(key, value);
} catch (e) {
throw e;
}
}
/// 获取字符串值
static Future<String> getString(
String key, {
String defaultValue = '',
}) async {
try {
final instance = await _getInstance();
final value = instance.getString(key) ?? defaultValue;
return value;
} catch (e) {
return defaultValue;
}
}
/// 存储整数值
static Future<void> setInt(String key, int value) async {
try {
final instance = await _getInstance();
await instance.setInt(key, value);
} catch (e) {
throw e;
}
}
/// 获取整数值
static Future<int> getInt(String key, {int defaultValue = 0}) async {
try {
final instance = await _getInstance();
final value = instance.getInt(key) ?? defaultValue;
return value;
} catch (e) {
return defaultValue;
}
}
/// 存储布尔值
static Future<void> setBool(String key, bool value) async {
try {
final instance = await _getInstance();
await instance.setBool(key, value);
} catch (e) {
throw e;
}
}
/// 获取布尔值
static Future<bool> getBool(String key, {bool defaultValue = false}) async {
try {
final instance = await _getInstance();
final value = instance.getBool(key) ?? defaultValue;
return value;
} catch (e) {
return defaultValue;
}
}
/// 存储双精度值
static Future<void> setDouble(String key, double value) async {
try {
final instance = await _getInstance();
await instance.setDouble(key, value);
} catch (e) {
throw e;
}
}
/// 获取双精度值
static Future<double> getDouble(
String key, {
double defaultValue = 0.0,
}) async {
try {
final instance = await _getInstance();
final value = instance.getDouble(key) ?? defaultValue;
return value;
} catch (e) {
return defaultValue;
}
}
/// 存储字符串列表
static Future<void> setStringList(String key, List<String> value) async {
try {
final instance = await _getInstance();
await instance.setStringList(key, value);
} catch (e) {
throw e;
}
}
/// 获取字符串列表
static Future<List<String>> getStringList(
String key, {
List<String>? defaultValue,
}) async {
try {
final instance = await _getInstance();
final value = instance.getStringList(key) ?? defaultValue ?? [];
return value;
} catch (e) {
return defaultValue ?? [];
}
}
/// 移除指定键的值
static Future<void> remove(String key) async {
try {
final instance = await _getInstance();
await instance.remove(key);
} catch (e) {
throw e;
}
}
/// 清空所有存储
static Future<void> clear() async {
try {
final instance = await _getInstance();
await instance.clear();
} catch (e) {
throw e;
}
}
/// 获取所有存储的键
static Future<Set<String>> getKeys() async {
try {
final instance = await _getInstance();
final keys = instance.getKeys();
return keys;
} catch (e) {
return {};
}
}
/// 检查键是否存在
static Future<bool> containsKey(String key) async {
try {
final instance = await _getInstance();
final exists = instance.containsKey(key);
return exists;
} catch (e) {
return false;
}
}
/// 重新加载数据
static Future<void> reload() async {
try {
final instance = await _getInstance();
await instance.reload();
} catch (e) {
// 忽略错误
}
}
}