cache_helper.dart 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import 'package:shared_preferences/shared_preferences.dart';
  2. class CacheHelper
  3. {
  4. static late SharedPreferences sharedPreferences;
  5. static init() async
  6. {
  7. sharedPreferences = await SharedPreferences.getInstance();
  8. }
  9. static Future<bool> putBool({required String key, required bool value})async
  10. {
  11. return await sharedPreferences.setBool(key, value);
  12. }
  13. static String getString({required String key})
  14. {
  15. return sharedPreferences.getString(key) ?? '';
  16. }
  17. static int getInteger({required String key})
  18. {
  19. return sharedPreferences.getInt(key) ?? 0;
  20. }
  21. static bool getBool({required String key})
  22. {
  23. return sharedPreferences.getBool(key) ?? false;
  24. }
  25. static Future<bool> saveData({required String key, required dynamic value}) async
  26. {
  27. if(value is String) {
  28. return await sharedPreferences.setString(key, value);
  29. } else if(value is bool) {
  30. return await sharedPreferences.setBool(key, value);
  31. } else if(value is int) {
  32. return await sharedPreferences.setInt(key, value);
  33. } else {
  34. return await sharedPreferences.setDouble(key, value);
  35. }
  36. }
  37. static Future<bool> removeData({required String key,}) async
  38. {
  39. return await sharedPreferences.remove(key);
  40. }
  41. }