storage.dart 872 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import 'dart:convert';
  2. import 'package:shared_preferences/shared_preferences.dart';
  3. class StorageUtil {
  4. StorageUtil._internal();
  5. static final StorageUtil _instance = StorageUtil._internal();
  6. factory StorageUtil(){
  7. return _instance;
  8. }
  9. SharedPreferences? prefs;
  10. Future<void> init() async{
  11. prefs = await SharedPreferences.getInstance();
  12. }
  13. Future<bool> setJSON(String key,var jsonVal){
  14. String jsonString = jsonEncode(jsonVal);
  15. return prefs!.setString(key, jsonString);
  16. }
  17. getJSON(String key){
  18. String? jsonString = prefs?.getString(key);
  19. return jsonString == null?null:jsonDecode(jsonString);
  20. }
  21. Future<bool> setBool(String key,bool value){
  22. return prefs!.setBool(key, value);
  23. }
  24. bool? getBool(String key){
  25. return prefs!.getBool(key);
  26. }
  27. Future<bool> remove(String key){
  28. return prefs!.remove(key);
  29. }
  30. }