基础库
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

58 lines
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. SharedPreferences prefs = await SharedPreferences.getInstance();
  32. if (null != prefs) {
  33. return prefs.setString(key, value);
  34. }
  35. // }
  36. return false;
  37. }
  38. static Future<String> getStringValue(String key, {String defaultVal = ''}) async {
  39. if (!EmptyUtil.isEmpty(key)) {
  40. SharedPreferences prefs = await SharedPreferences.getInstance();
  41. if (null != prefs && prefs.containsKey(key)) {
  42. return prefs.getString(key);
  43. }
  44. }
  45. return defaultVal;
  46. }
  47. static Future<bool> clean() async {
  48. SharedPreferences prefs = await SharedPreferences.getInstance();
  49. return await prefs.clear();
  50. }
  51. }