Flutter中的SharePreferences单例化

2022年8月29日 775点热度 0人点赞 0条评论
<code>library sp_util;

import &#039;dart:async&#039;;
import &#039;dart:convert&#039;;

import &#039;package:shared_preferences/shared_preferences.dart&#039;;
import &#039;package:synchronized/synchronized.dart&#039;;

class SpUtil {
  static SpUtil? _singleton;
  static SharedPreferences? _prefs;
  static Lock _lock = Lock();

  static Future&lt;SpUtil?&gt; getInstance() async {
    if (_singleton == null) {
      await _lock.synchronized(() async {
        if (_singleton == null) {
          // keep local instance till it is fully initialized.
          // 保持本地实例直到完全初始化。
          var singleton = SpUtil._();
          await singleton._init();
          _singleton = singleton;
        }
      });
    }
    return _singleton;
  }

  SpUtil._();

  Future _init() async {
    _prefs = await SharedPreferences.getInstance();
  }

  /// put object.
  static Future&lt;bool&gt;? putObject(String key, Object value) {
    return _prefs?.setString(key, json.encode(value));
  }

  /// get obj.
  static T? getObj&lt;T&gt;(String key, T f(Map v), {T? defValue}) {
    Map? map = getObject(key);
    return map == null ? defValue : f(map);
  }

  /// get object.
  static Map? getObject(String key) {
    String? _data = _prefs?.getString(key);
    return (_data == null || _data.isEmpty) ? null : json.decode(_data);
  }

  /// put object list.
  static Future&lt;bool&gt;? putObjectList(String key, List&lt;Object&gt; list) {
    List&lt;String&gt;? _dataList = list.map((value) {
      return json.encode(value);
    }).toList();
    return _prefs?.setStringList(key, _dataList);
  }

  /// get obj list.
  static List&lt;T&gt;? getObjList&lt;T&gt;(String key, T f(Map v),
      {List&lt;T&gt;? defValue = const []}) {
    List&lt;Map&gt;? dataList = getObjectList(key);
    List&lt;T&gt;? list = dataList?.map((value) {
      return f(value);
    }).toList();
    return list ?? defValue;
  }

  /// get object list.
  static List&lt;Map&gt;? getObjectList(String key) {
    List&lt;String&gt;? dataLis = _prefs?.getStringList(key);
    return dataLis?.map((value) {
      Map _dataMap = json.decode(value);
      return _dataMap;
    }).toList();
  }

  /// get string.
  static String? getString(String key, {String? defValue = &#039;&#039;}) {
    return _prefs?.getString(key) ?? defValue;
  }

  /// put string.
  static Future&lt;bool&gt;? putString(String key, String value) {
    return _prefs?.setString(key, value);
  }

  /// get bool.
  static bool? getBool(String key, {bool? defValue = false}) {
    return _prefs?.getBool(key) ?? defValue;
  }

  /// put bool.
  static Future&lt;bool&gt;? putBool(String key, bool value) {
    return _prefs?.setBool(key, value);
  }

  /// get int.
  static int? getInt(String key, {int? defValue = 0}) {
    return _prefs?.getInt(key) ?? defValue;
  }

  /// put int.
  static Future&lt;bool&gt;? putInt(String key, int value) {
    return _prefs?.setInt(key, value);
  }

  /// get double.
  static double? getDouble(String key, {double? defValue = 0.0}) {
    return _prefs?.getDouble(key) ?? defValue;
  }

  /// put double.
  static Future&lt;bool&gt;? putDouble(String key, double value) {
    return _prefs?.setDouble(key, value);
  }

  /// get string list.
  static List&lt;String&gt;? getStringList(String key,
      {List&lt;String&gt;? defValue = const []}) {
    return _prefs?.getStringList(key) ?? defValue;
  }

  /// put string list.
  static Future&lt;bool&gt;? putStringList(String key, List&lt;String&gt; value) {
    return _prefs?.setStringList(key, value);
  }

  /// get dynamic.
  static dynamic getDynamic(String key, {Object? defValue}) {
    return _prefs?.get(key) ?? defValue;
  }

  /// have key.
  static bool? haveKey(String key) {
    return _prefs?.getKeys().contains(key);
  }

  /// contains Key.
  static bool? containsKey(String key) {
    return _prefs?.containsKey(key);
  }

  /// get keys.
  static Set&lt;String&gt;? getKeys() {
    return _prefs?.getKeys();
  }

  /// remove.
  static Future&lt;bool&gt;? remove(String key) {
    return _prefs?.remove(key);
  }

  /// clear.
  static Future&lt;bool&gt;? clear() {
    return _prefs?.clear();
  }

  /// Fetches the latest values from the host platform.
  static Future&lt;void&gt;? reload() {
    return _prefs?.reload();
  }

  ///Sp is initialized.
  static bool isInitialized() {
    return _prefs != null;
  }

  /// get Sp.
  static SharedPreferences? getSp() {
    return _prefs;
  }
}</code>

小小调酒师

此刻打盹,你将做梦; 此刻学习,你将圆梦。 个人邮箱:shellways@foxmail.com

文章评论