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