sp_util.dart 4.7 KB

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