sp_util.dart 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 object.
  42. static Map getObject(String key) {
  43. if (_prefs == null) return null;
  44. String _data = _prefs.getString(key);
  45. return (_data == null || _data.isEmpty) ? null : json.decode(_data);
  46. }
  47. /// put object list.
  48. static Future<bool> putObjectList(String key, List<Object> list) {
  49. if (_prefs == null) return null;
  50. List<String> _dataList = list?.map((value) {
  51. return json.encode(value);
  52. })?.toList();
  53. return _prefs.setStringList(key, _dataList);
  54. }
  55. /// get object list.
  56. static List<Map> getObjectList(String key) {
  57. if (_prefs == null) return null;
  58. List<String> dataLis = _prefs.getStringList(key);
  59. return dataLis?.map((value) {
  60. Map _dataMap = json.decode(value);
  61. return _dataMap;
  62. })?.toList();
  63. }
  64. /// get string.
  65. static String getString(String key, {String defValue = ''}) {
  66. if (_prefs == null) return defValue;
  67. return _prefs.getString(key) ?? defValue;
  68. }
  69. /// put string.
  70. static Future<bool> putString(String key, String value) {
  71. if (_prefs == null) return null;
  72. return _prefs.setString(key, value);
  73. }
  74. /// get bool.
  75. static bool getBool(String key, {bool defValue = false}) {
  76. if (_prefs == null) return defValue;
  77. return _prefs.getBool(key) ?? defValue;
  78. }
  79. /// put bool.
  80. static Future<bool> putBool(String key, bool value) {
  81. if (_prefs == null) return null;
  82. return _prefs.setBool(key, value);
  83. }
  84. /// get int.
  85. static int getInt(String key, {int defValue = 0}) {
  86. if (_prefs == null) return defValue;
  87. return _prefs.getInt(key) ?? defValue;
  88. }
  89. /// put int.
  90. static Future<bool> putInt(String key, int value) {
  91. if (_prefs == null) return null;
  92. return _prefs.setInt(key, value);
  93. }
  94. /// get double.
  95. static double getDouble(String key, {double defValue = 0.0}) {
  96. if (_prefs == null) return defValue;
  97. return _prefs.getDouble(key) ?? defValue;
  98. }
  99. /// put double.
  100. static Future<bool> putDouble(String key, double value) {
  101. if (_prefs == null) return null;
  102. return _prefs.setDouble(key, value);
  103. }
  104. /// get string list.
  105. static List<String> getStringList(String key,
  106. {List<String> defValue = const []}) {
  107. if (_prefs == null) return defValue;
  108. return _prefs.getStringList(key) ?? defValue;
  109. }
  110. /// put string list.
  111. static Future<bool> putStringList(String key, List<String> value) {
  112. if (_prefs == null) return null;
  113. return _prefs.setStringList(key, value);
  114. }
  115. /// get dynamic.
  116. static dynamic getDynamic(String key, {Object defValue}) {
  117. if (_prefs == null) return defValue;
  118. return _prefs.get(key) ?? defValue;
  119. }
  120. /// have key.
  121. static bool haveKey(String key) {
  122. if (_prefs == null) return null;
  123. return _prefs.getKeys().contains(key);
  124. }
  125. /// get keys.
  126. static Set<String> getKeys() {
  127. if (_prefs == null) return null;
  128. return _prefs.getKeys();
  129. }
  130. /// remove.
  131. static Future<bool> remove(String key) {
  132. if (_prefs == null) return null;
  133. return _prefs.remove(key);
  134. }
  135. /// clear.
  136. static Future<bool> clear() {
  137. if (_prefs == null) return null;
  138. return _prefs.clear();
  139. }
  140. ///Sp is initialized.
  141. static bool isInitialized() {
  142. return _prefs != null;
  143. }
  144. }