HiCache._pre(prefs); 报错
来源:3-11 统一缓存管理框架hi_cache设计

lyz0
2021-07-13
报错:
Instance member '_pre' can't be accessed using static access.
完整代码
import 'package:shared_preferences/shared_preferences.dart';
///缓存管理
class HiCache {
SharedPreferences prefs;
HiCache._() {
init();
}
static HiCache _instance;
HiCache _pre(SharedPreferences prefs) {
this.prefs = prefs;
}
///预初始化,防止使用get时,prefs还未初始化
static Future<HiCache> preInit() async {
if (_instance == null) {
var prefs = await SharedPreferences.getInstance();
_instance = HiCache._pre(prefs);
}
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<String> value) {
prefs.setStringList(key, value);
}
T get<T>(String key) {
return prefs.get(key);
}
}
写回答
1回答
-
CrazyCodeBoy
2021-07-13
_pre方法写法不对,改成
HiCache._pre(SharedPreferences prefs) {this.prefs = prefs;
}
可参考下:https://git.imooc.com/coding-487/fa-pub/src/master/component/hi_cache/lib/hi_cache.dart
00
相似问题