SpUtils class

SharedPreferences 薄封装单例。

putJsonable / getObject 系列把可 JSON 编码的对象序列化后落地; putString / getBool 等基本类型直接转发到 SharedPreferences。 日志只记录 key(不记录 value),避免 token / PII 入日志。 并发 init 由 single-flight Future 去重。

Constructors

SpUtils()
factory

Properties

hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

clear() → Future<bool>
清空所有数据
containsKey(String key) → bool
是否包含指定键
ensureInitialized() → Future<void>
确保 init 已完成;若尚未初始化则等待其完成。供各 get/put 方法内部使用。
getBool(String key, {bool defValue = false}) → bool?
获取布尔值
getDouble(String key, {double defValue = 0.0}) → double?
获取浮点数
getInt(String key, {int defValue = 0}) → int?
获取整数
getKeys() → Set<String>?
获取所有键
getObject<T>(String key, T fromJson(Map<String, dynamic> json), {T? defValue}) → T?
获取对象
getObjectList<T>(String key, T fromJson(Map<String, dynamic> json), {List<T>? defValue = const []}) → List<T>?
获取对象列表
getSp() → SharedPreferences?
获取原始 SharedPreferences 实例
getString(String key, {String? defValue = ''}) → String?
获取字符串
getStringList(String key, {List<String>? defValue = const []}) → List<String>?
获取字符串列表
init() → Future<bool>
初始化 SharedPreferences(single-flight:并发调用共享同一次 getInstance)。
isInitialized() → bool
检查是否已初始化
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
putBool(String key, bool value) → Future<bool>
存储布尔值
putDouble(String key, double value) → Future<bool>
存储浮点数
putInt(String key, int value) → Future<bool>
存储整数
putJsonable(String key, Object value) → Future<bool>
存储「可 JSON 编码」的对象(Map / 带 toJson 的对象 / List / 基本类型)。
putObject(String key, Object value) → Future<bool>
Deprecated:使用 putJsonable 代替。
putObjectList(String key, List<Object> list) → Future<bool>
存储对象列表
putString(String key, String value) → Future<bool>
存储字符串
putStringList(String key, List<String> value) → Future<bool>
存储字符串列表
reload() → Future<void>
重新加载数据(用于调试或热更新)
remove(String key) → Future<bool>
移除某个键
resetInstanceForTesting() → void
仅用于测试:重置单例持有的 SharedPreferences 缓存与 in-flight Future, 使下一个 init() 走冷启动路径。生产代码不得调用。
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited