sp_util.dart 4.2 KB

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