基础库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

60 line
1.8 KiB

  1. import 'dart:convert';
  2. import 'package:shared_preferences/shared_preferences.dart';
  3. import 'package:zhiying_comm/util/empty_util.dart';
  4. import 'package:zhiying_comm/util/log/let_log.dart';
  5. class SharedPreferencesUtil {
  6. static Future<Map<String, dynamic>> getNetCacheResult(String key) async {
  7. if (!EmptyUtil.isEmpty(key)) {
  8. SharedPreferences prefs = await SharedPreferences.getInstance();
  9. String cacheResult = prefs.getString(key);
  10. // TODO need解密?
  11. if (!EmptyUtil.isEmpty(cacheResult)) {
  12. Map<String, dynamic> map = json.decode(cacheResult);
  13. if (!EmptyUtil.isEmpty(map)) {
  14. return map;
  15. }
  16. }
  17. }
  18. return null;
  19. }
  20. static Future<void> setNetCacheResult(String key, String value) async {
  21. if (!EmptyUtil.isEmpty(key) && !EmptyUtil.isEmpty(value)) {
  22. SharedPreferences prefs = await SharedPreferences.getInstance();
  23. // TODO need加密?
  24. prefs.setString(key, value);
  25. }
  26. return;
  27. }
  28. static Future<bool> setStringValue(String key, String value) async {
  29. // if (!EmptyUtil.isEmpty(key) && !EmptyUtil.isEmpty(value)) {
  30. Logger.log('key = ${key}, value = $value');
  31. print("key:" + (key ?? "") + " value:" + (value ?? ""));
  32. SharedPreferences prefs = await SharedPreferences.getInstance();
  33. if (null != prefs) {
  34. return prefs.setString(key, value);
  35. }
  36. // }
  37. return false;
  38. }
  39. static Future<String> getStringValue(String key, {String defaultVal = ''}) async {
  40. if (!EmptyUtil.isEmpty(key)) {
  41. SharedPreferences prefs = await SharedPreferences.getInstance();
  42. if (null != prefs && prefs.containsKey(key)) {
  43. return prefs.getString(key);
  44. }
  45. }
  46. return defaultVal;
  47. }
  48. static Future<bool> clean() async {
  49. SharedPreferences prefs = await SharedPreferences.getInstance();
  50. return await prefs.clear();
  51. }
  52. }