sp_util.dart 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import 'dart:async';
  2. import 'package:shared_preferences/shared_preferences.dart';
  3. import 'package:synchronized/synchronized.dart';
  4. /**
  5. * @Author: thl
  6. * @GitHub: https://github.com/Sky24n
  7. * @JianShu: https://www.jianshu.com/u/cbf2ad25d33a
  8. * @Email: 863764940@qq.com
  9. * @Description: Sp Util.
  10. * @Date: 2018/9/8
  11. */
  12. /// SharedPreferences Util.
  13. class SpUtil {
  14. static SpUtil _singleton;
  15. static SharedPreferences _prefs;
  16. static Lock _lock = Lock();
  17. static Future<SpUtil> getInstance() async {
  18. if (_singleton == null) {
  19. await _lock.synchronized(() async {
  20. if (_singleton == null) {
  21. // keep local instance till it is fully initialized.
  22. // 保持本地实例直到完全初始化。
  23. var singleton = SpUtil._();
  24. await singleton._init();
  25. _singleton = singleton;
  26. }
  27. });
  28. }
  29. return _singleton;
  30. }
  31. SpUtil._();
  32. Future _init() async {
  33. _prefs = await SharedPreferences.getInstance();
  34. }
  35. static String getString(String key) {
  36. if (_prefs == null) return null;
  37. return _prefs.getString(key);
  38. }
  39. static Future<bool> putString(String key, String value) {
  40. if (_prefs == null) return null;
  41. return _prefs.setString(key, value);
  42. }
  43. static bool getBool(String key) {
  44. if (_prefs == null) return null;
  45. return _prefs.getBool(key);
  46. }
  47. static Future<bool> putBool(String key, bool value) {
  48. if (_prefs == null) return null;
  49. return _prefs.setBool(key, value);
  50. }
  51. static int getInt(String key) {
  52. if (_prefs == null) return null;
  53. return _prefs.getInt(key);
  54. }
  55. static Future<bool> putInt(String key, int value) {
  56. if (_prefs == null) return null;
  57. return _prefs.setInt(key, value);
  58. }
  59. static double getDouble(String key) {
  60. if (_prefs == null) return null;
  61. return _prefs.getDouble(key);
  62. }
  63. static Future<bool> putDouble(String key, double value) {
  64. if (_prefs == null) return null;
  65. return _prefs.setDouble(key, value);
  66. }
  67. static List<String> getStringList(String key) {
  68. if (_prefs == null) return null;
  69. return _prefs.getStringList(key);
  70. }
  71. static Future<bool> putStringList(String key, List<String> value) {
  72. if (_prefs == null) return null;
  73. return _prefs.setStringList(key, value);
  74. }
  75. static dynamic getDynamic(String key) {
  76. if (_prefs == null) return null;
  77. return _prefs.get(key);
  78. }
  79. static Set<String> getKeys() {
  80. if (_prefs == null) return null;
  81. return _prefs.getKeys();
  82. }
  83. static Future<bool> remove(String key) {
  84. if (_prefs == null) return null;
  85. return _prefs.remove(key);
  86. }
  87. static Future<bool> clear() {
  88. if (_prefs == null) return null;
  89. return _prefs.clear();
  90. }
  91. ///Sp is initialized.
  92. static bool isInitialized() {
  93. return _prefs != null;
  94. }
  95. }