mmkv_util.dart 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import 'package:mmkv/mmkv.dart';
  2. class KVUtil {
  3. KVUtil._();
  4. static MMKV? _mmkv;
  5. static init() async {
  6. if (_mmkv != null) {
  7. return;
  8. }
  9. await MMKV.initialize();
  10. _mmkv = MMKV.defaultMMKV();
  11. }
  12. static void putString(String key, String? value) {
  13. _getMMKV().encodeString(key, value);
  14. }
  15. static String? getString(String key, String? defaultValue) {
  16. return _getMMKV().decodeString(key) ?? defaultValue;
  17. }
  18. static void putInt(String key, int value) {
  19. _getMMKV().encodeInt(key, value);
  20. }
  21. static int getInt(String key, int defaultValue) {
  22. return _getMMKV().decodeInt(key, defaultValue: defaultValue);
  23. }
  24. static void putBool(String key, bool value) {
  25. _getMMKV().encodeBool(key, value);
  26. }
  27. static bool getBool(String key, bool defaultValue) {
  28. return _getMMKV().decodeBool(key, defaultValue: defaultValue);
  29. }
  30. static void putDouble(String key, double value) {
  31. _getMMKV().encodeDouble(key, value);
  32. }
  33. static double getDouble(String key, double defaultValue) {
  34. return _getMMKV().decodeDouble(key, defaultValue: defaultValue);
  35. }
  36. static MMKV _getMMKV() {
  37. _mmkv ??= MMKV.defaultMMKV();
  38. return _mmkv!;
  39. }
  40. }