sp_util.dart 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import 'package:shared_preferences/shared_preferences.dart';
  2. class SpUtil {
  3. static Future<SharedPreferences> _getInstance() {
  4. return SharedPreferences.getInstance();
  5. }
  6. static Future<bool> contains(String key) async {
  7. SharedPreferences sp = await _getInstance();
  8. return sp.containsKey(key);
  9. }
  10. static Future<bool> remove(String key) async {
  11. SharedPreferences sp = await _getInstance();
  12. return sp.remove(key);
  13. }
  14. static Future<bool> setInt(String key, int value) async {
  15. SharedPreferences sp = await _getInstance();
  16. return sp.setInt(key, value);
  17. }
  18. static Future<bool> setDouble(String key, double value) async {
  19. SharedPreferences sp = await _getInstance();
  20. return sp.setDouble(key, value);
  21. }
  22. static Future<bool> setBool(String key, bool value) async {
  23. SharedPreferences sp = await _getInstance();
  24. return sp.setBool(key, value);
  25. }
  26. static Future<bool> setString(String key, String value) async {
  27. SharedPreferences sp = await _getInstance();
  28. return sp.setString(key, value);
  29. }
  30. static Future<bool> setStringList(String key, List<String> value) async {
  31. SharedPreferences sp = await _getInstance();
  32. return sp.setStringList(key, value);
  33. }
  34. static Future<int> getInt(String key, {int defValue = 0}) async {
  35. SharedPreferences sp = await _getInstance();
  36. return sp.getInt(key) ?? defValue;
  37. }
  38. static Future<double> getDouble(String key, {double defValue = 0.0}) async {
  39. SharedPreferences sp = await _getInstance();
  40. return sp.getDouble(key) ?? defValue;
  41. }
  42. static Future<bool> getBool(String key, {bool defValue = false}) async {
  43. SharedPreferences sp = await _getInstance();
  44. return sp.getBool(key) ?? defValue;
  45. }
  46. static Future<String> getString(String key, {String defValue = ""}) async {
  47. SharedPreferences sp = await _getInstance();
  48. return sp.getString(key) ?? defValue;
  49. }
  50. static Future<List<String>> getStringList(String key) async {
  51. SharedPreferences sp = await _getInstance();
  52. return sp.getStringList(key) ?? [];
  53. }
  54. static Future<bool> getEnableDelayShot() {
  55. return getBool("delayShot", defValue: false);
  56. }
  57. static Future<bool> setEnabelDelayShot(bool bo) {
  58. return setBool("delayShot", bo);
  59. }
  60. static Future<int> getDelayShotTime() {
  61. return getInt("delayShotTime", defValue: 2);
  62. }
  63. static Future<bool> setDelayShotTime(int value) {
  64. return setInt("delayShotTime", value);
  65. }
  66. static Future<String> getUserId() {
  67. return getString("userId", defValue: "");
  68. }
  69. static Future<bool> setUserId(String userName) {
  70. return setString("userId", userName);
  71. }
  72. static Future<String> getUserName() {
  73. return getString("userName", defValue: "");
  74. }
  75. static Future<bool> setUserName(String userName) {
  76. return setString("userName", userName);
  77. }
  78. static Future<String> getToken() {
  79. return getString("token", defValue: "");
  80. }
  81. static Future<bool> setToken(String token) {
  82. return setString("token", token);
  83. }
  84. static Future<int> getSelectedPatientId() {
  85. return getInt("selectedPatientId", defValue: -1);
  86. }
  87. static Future<bool> setSelectedPatientId(int id) {
  88. return setInt("selectedPatientId", id);
  89. }
  90. static Future<bool> isAppStartAgreementAgreed() {
  91. return getBool("appStartAgreementAgreed", defValue: false);
  92. }
  93. static Future<bool> setAppStartAgreementAgreed(bool bo) {
  94. return setBool("appStartAgreementAgreed", bo);
  95. }
  96. static Future<bool> hasStoragePermissionRequested() {
  97. return getBool("storagePermissionRequested", defValue: false);
  98. }
  99. static Future<bool> setStoragePermissionRequested(bool bo) {
  100. return setBool("storagePermissionRequested", bo);
  101. }
  102. static Future<bool> hasLocationPermissionRequested() {
  103. return getBool("locationPermissionRequested", defValue: false);
  104. }
  105. static Future<bool> setLocationPermissionRequested(bool bo) {
  106. return setBool("locationPermissionRequested", bo);
  107. }
  108. }