sp_util.dart 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. library sp_util;
  2. import 'dart:async';
  3. import 'dart:convert';
  4. import 'package:shared_preferences/shared_preferences.dart';
  5. import 'package:synchronized/synchronized.dart';
  6. /// SharedPreferences Util.
  7. class SPUtil {
  8. static SPUtil? _singleton;
  9. static SharedPreferences? _prefs;
  10. static Lock _lock = Lock();
  11. static Future<SPUtil?> getInstance() async {
  12. if (_singleton == null) {
  13. await _lock.synchronized(() async {
  14. if (_singleton == null) {
  15. // keep local instance till it is fully initialized.
  16. // 保持本地实例直到完全初始化。
  17. var singleton = SPUtil._();
  18. await singleton._init();
  19. _singleton = singleton;
  20. }
  21. });
  22. }
  23. return _singleton;
  24. }
  25. SPUtil._();
  26. Future _init() async {
  27. _prefs = await SharedPreferences.getInstance();
  28. }
  29. /// put object.
  30. static Future<bool>? putObject(String key, Object value) {
  31. return _prefs?.setString(key, json.encode(value));
  32. }
  33. /// get obj.
  34. static T? getObj<T>(String key, T f(Map v), {T? defValue}) {
  35. Map? map = getObject(key);
  36. return map == null ? defValue : f(map);
  37. }
  38. /// get object.
  39. static Map? getObject(String key) {
  40. String? _data = _prefs?.getString(key);
  41. return (_data == null || _data.isEmpty) ? null : json.decode(_data);
  42. }
  43. /// put object list.
  44. static Future<bool>? putObjectList(String key, List<Object> list) {
  45. List<String>? _dataList = list.map((value) {
  46. return json.encode(value);
  47. }).toList();
  48. return _prefs?.setStringList(key, _dataList);
  49. }
  50. /// get obj list.
  51. static List<T>? getObjList<T>(String key, T f(Map v),
  52. {List<T>? defValue = const []}) {
  53. List<Map>? dataList = getObjectList(key);
  54. List<T>? list = dataList?.map((value) {
  55. return f(value);
  56. }).toList();
  57. return list ?? defValue;
  58. }
  59. /// get object list.
  60. static List<Map>? getObjectList(String key) {
  61. List<String>? dataLis = _prefs?.getStringList(key);
  62. return dataLis?.map((value) {
  63. Map _dataMap = json.decode(value);
  64. return _dataMap;
  65. }).toList();
  66. }
  67. /// get string.
  68. static String? getString(String key, {String? defValue = ''}) {
  69. return _prefs?.getString(key) ?? defValue;
  70. }
  71. /// put string.
  72. static Future<bool>? putString(String key, String value) {
  73. return _prefs?.setString(key, value);
  74. }
  75. /// get bool.
  76. static bool? getBool(String key, {bool? defValue = false}) {
  77. return _prefs?.getBool(key) ?? defValue;
  78. }
  79. /// put bool.
  80. static Future<bool>? putBool(String key, bool value) {
  81. return _prefs?.setBool(key, value);
  82. }
  83. /// get int.
  84. static int? getInt(String key, {int? defValue = 0}) {
  85. return _prefs?.getInt(key) ?? defValue;
  86. }
  87. /// put int.
  88. static Future<bool>? putInt(String key, int value) {
  89. return _prefs?.setInt(key, value);
  90. }
  91. /// get double.
  92. static double? getDouble(String key, {double? defValue = 0.0}) {
  93. return _prefs?.getDouble(key) ?? defValue;
  94. }
  95. /// put double.
  96. static Future<bool>? putDouble(String key, double value) {
  97. return _prefs?.setDouble(key, value);
  98. }
  99. /// get string list.
  100. static List<String>? getStringList(String key,
  101. {List<String>? defValue = const []}) {
  102. return _prefs?.getStringList(key) ?? defValue;
  103. }
  104. /// put string list.
  105. static Future<bool>? putStringList(String key, List<String> value) {
  106. return _prefs?.setStringList(key, value);
  107. }
  108. /// get dynamic.
  109. static dynamic getDynamic(String key, {Object? defValue}) {
  110. return _prefs?.get(key) ?? defValue;
  111. }
  112. /// have key.
  113. static bool? haveKey(String key) {
  114. return _prefs?.getKeys().contains(key);
  115. }
  116. /// contains Key.
  117. static bool? containsKey(String key) {
  118. return _prefs?.containsKey(key);
  119. }
  120. /// get keys.
  121. static Set<String>? getKeys() {
  122. return _prefs?.getKeys();
  123. }
  124. /// remove.
  125. static Future<bool>? remove(String key) {
  126. return _prefs?.remove(key);
  127. }
  128. /// clear.
  129. static Future<bool>? clear() {
  130. return _prefs?.clear();
  131. }
  132. /// Fetches the latest values from the host platform.
  133. static Future<void>? reload() {
  134. return _prefs?.reload();
  135. }
  136. ///Sp is initialized.
  137. static bool isInitialized() {
  138. return _prefs != null;
  139. }
  140. /// get Sp.
  141. static SharedPreferences? getSp() {
  142. return _prefs;
  143. }
  144. }