local_storage.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import 'package:shared_preferences/shared_preferences.dart';
  2. import 'package:sino_med_cloud/core/constants/app_constants.dart';
  3. /// 本地存储管理类
  4. class LocalStorage {
  5. static SharedPreferences? _prefs;
  6. /// 初始化本地存储
  7. static Future<void> init() async {
  8. _prefs ??= await SharedPreferences.getInstance();
  9. }
  10. /// 获取 SharedPreferences 实例
  11. static SharedPreferences get prefs {
  12. if (_prefs == null) {
  13. throw Exception('LocalStorage not initialized. Call LocalStorage.init() first.');
  14. }
  15. return _prefs!;
  16. }
  17. // ==================== String 操作 ====================
  18. /// 保存字符串
  19. static Future<bool> setString(String key, String value) async {
  20. return await prefs.setString(key, value);
  21. }
  22. /// 获取字符串
  23. static String? getString(String key, {String? defaultValue}) {
  24. return prefs.getString(key) ?? defaultValue;
  25. }
  26. // ==================== Int 操作 ====================
  27. /// 保存整数
  28. static Future<bool> setInt(String key, int value) async {
  29. return await prefs.setInt(key, value);
  30. }
  31. /// 获取整数
  32. static int? getInt(String key, {int? defaultValue}) {
  33. return prefs.getInt(key) ?? defaultValue;
  34. }
  35. // ==================== Double 操作 ====================
  36. /// 保存浮点数
  37. static Future<bool> setDouble(String key, double value) async {
  38. return await prefs.setDouble(key, value);
  39. }
  40. /// 获取浮点数
  41. static double? getDouble(String key, {double? defaultValue}) {
  42. return prefs.getDouble(key) ?? defaultValue;
  43. }
  44. // ==================== Bool 操作 ====================
  45. /// 保存布尔值
  46. static Future<bool> setBool(String key, bool value) async {
  47. return await prefs.setBool(key, value);
  48. }
  49. /// 获取布尔值
  50. static bool? getBool(String key, {bool? defaultValue}) {
  51. return prefs.getBool(key) ?? defaultValue;
  52. }
  53. // ==================== StringList 操作 ====================
  54. /// 保存字符串列表
  55. static Future<bool> setStringList(String key, List<String> value) async {
  56. return await prefs.setStringList(key, value);
  57. }
  58. /// 获取字符串列表
  59. static List<String>? getStringList(String key) {
  60. return prefs.getStringList(key);
  61. }
  62. // ==================== 通用操作 ====================
  63. /// 删除指定 key
  64. static Future<bool> remove(String key) async {
  65. return await prefs.remove(key);
  66. }
  67. /// 清空所有数据
  68. static Future<bool> clear() async {
  69. return await prefs.clear();
  70. }
  71. /// 检查 key 是否存在
  72. static bool containsKey(String key) {
  73. return prefs.containsKey(key);
  74. }
  75. /// 获取所有 key
  76. static Set<String> getKeys() {
  77. return prefs.getKeys();
  78. }
  79. // ==================== 常用业务方法 ====================
  80. /// 保存 Token
  81. static Future<bool> saveToken(String token) async {
  82. return await setString(AppConstants.keyToken, token);
  83. }
  84. /// 获取 Token
  85. static String? getToken() {
  86. return getString(AppConstants.keyToken);
  87. }
  88. /// 删除 Token
  89. static Future<bool> removeToken() async {
  90. return await remove(AppConstants.keyToken);
  91. }
  92. /// 保存用户信息(JSON 字符串)
  93. static Future<bool> saveUserInfo(String userInfo) async {
  94. return await setString(AppConstants.keyUserInfo, userInfo);
  95. }
  96. /// 获取用户信息
  97. static String? getUserInfo() {
  98. return getString(AppConstants.keyUserInfo);
  99. }
  100. /// 删除用户信息
  101. static Future<bool> removeUserInfo() async {
  102. return await remove(AppConstants.keyUserInfo);
  103. }
  104. }