sp_util.dart 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. * @Description: Sp Util.
  8. * @Date: 2018/9/8
  9. */
  10. ///
  11. class SpUtil {
  12. static SpUtil _singleton;
  13. static SharedPreferences _sharedPreferences;
  14. static Lock _lock = Lock();
  15. static Future<SpUtil> getInstance() async {
  16. if (_singleton == null) {
  17. await _lock.synchronized(() async {
  18. _singleton = new SpUtil();
  19. await init();
  20. });
  21. }
  22. return _singleton;
  23. }
  24. static void init() async {
  25. _sharedPreferences = await SharedPreferences.getInstance();
  26. }
  27. static String getString(String key) {
  28. return _sharedPreferences.getString(key);
  29. }
  30. static Future<bool> putString(String key, String value) {
  31. return _sharedPreferences.setString(key, value);
  32. }
  33. static bool getBool(String key) {
  34. return _sharedPreferences.getBool(key);
  35. }
  36. static Future<bool> putBool(String key, bool value) {
  37. return _sharedPreferences.setBool(key, value);
  38. }
  39. static int getInt(String key) {
  40. return _sharedPreferences.getInt(key);
  41. }
  42. static Future<bool> putInt(String key, int value) {
  43. return _sharedPreferences.setInt(key, value);
  44. }
  45. static double getDouble(String key) {
  46. return _sharedPreferences.getDouble(key);
  47. }
  48. static Future<bool> putDouble(String key, double value) {
  49. return _sharedPreferences.setDouble(key, value);
  50. }
  51. static List<String> getStringList(String key) {
  52. return _sharedPreferences.getStringList(key);
  53. }
  54. static Future<bool> putStringList(String key, List<String> value) {
  55. return _sharedPreferences.setStringList(key, value);
  56. }
  57. static dynamic getDynamic(String key) {
  58. return _sharedPreferences.get(key);
  59. }
  60. static Set<String> getKeys() {
  61. return _sharedPreferences.getKeys();
  62. }
  63. static Future<bool> remove(String key) {
  64. return _sharedPreferences.remove(key);
  65. }
  66. static Future<bool> clear() {
  67. return _sharedPreferences.clear();
  68. }
  69. }