import 'dart:async'; import 'dart:convert'; import 'package:shared_preferences/shared_preferences.dart'; class SpUtil { static SpUtil? _singleton; static SharedPreferences? _prefs; static Future getInstance() async { if (_singleton == null) { if (_singleton == null) { var singleton = SpUtil._(); await singleton._init(); _singleton = singleton; } } return _singleton; } SpUtil._(); Future _init() async { _prefs = await SharedPreferences.getInstance(); } /// put object. static Future? putObject(String key, Object value) { return _prefs?.setString(key, json.encode(value)); } /// get obj. static T? getObj(String key, T Function(Map v) f, {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? putObjectList(String key, List list) { List? dataList = list.map((value) { return json.encode(value); }).toList(); return _prefs?.setStringList(key, dataList); } /// get obj list. static List? getObjList(String key, T Function(Map v) f, {List? defValue = const []}) { List? dataList = getObjectList(key); List? list = dataList?.map((value) { return f(value); }).toList(); return list ?? defValue; } /// get object list. static List? getObjectList(String key) { List? 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 = ''}) { return _prefs?.getString(key) ?? defValue; } /// put string. static Future? 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? 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? 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? putDouble(String key, double value) { return _prefs?.setDouble(key, value); } /// get string list. static List? getStringList(String key, {List? defValue = const []}) { return _prefs?.getStringList(key) ?? defValue; } /// put string list. static Future? putStringList(String key, List 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? getKeys() { return _prefs?.getKeys(); } /// remove. static Future? remove(String key) { return _prefs?.remove(key); } /// clear. static Future? clear() { return _prefs?.clear(); } /// Fetches the latest values from the host platform. static Future? reload() { return _prefs?.reload(); } ///Sp is initialized. static bool isInitialized() { return _prefs != null; } /// get Sp. static SharedPreferences? getSp() { return _prefs; } }