HiCache._pre没有返回值 为何能对_instance赋值?
来源:5-5 Flutter新版路由和导航系统实战(二)

慕先生3072650
2021-11-16
///缓存管理类
class HiCache {
SharedPreferences? prefs;
HiCache._() {
init();
}
static HiCache? _instance;
HiCache._pre(SharedPreferences prefs) {
this.prefs = prefs;
}
///预初始化,防止在使用get时,prefs还未完成初始化
static Future preInit() async {
if (_instance == null) {
var prefs = await SharedPreferences.getInstance();
_instance = HiCache._pre(prefs);//这里没有返回任何东西 为何能对_instance赋值 不是null吗
}
return _instance!;
}
static HiCache getInstance() {
if (_instance == null) {
instance = HiCache.();
}
return _instance!;
}
void init() async {
if (prefs == null) {
prefs = await SharedPreferences.getInstance();
}
}
setString(String key, String value) {
prefs?.setString(key, value);
}
setDouble(String key, double value) {
prefs?.setDouble(key, value);
}
setInt(String key, int value) {
prefs?.setInt(key, value);
}
setBool(String key, bool value) {
prefs?.setBool(key, value);
}
setStringList(String key, List value) {
prefs?.setStringList(key, value);
}
remove(String key) {
prefs?.remove(key);
}
T? get(String key) {
var result = prefs?.get(key);
if (result != null) {
return result as T;
}
return null;
}
}
1回答
-
CrazyCodeBoy
2021-11-17
HiCache._pre是命名构造方法,是构造方法的一种,构造方法自带返回值 。
10
相似问题