sp_util.dart 3.9 KB

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