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