【发布时间】:2020-09-22 19:28:30
【问题描述】:
我有我的 ChangeNotifier 类,它应该触发注销会话跟踪,但它未能工作,实现这个的更清洁的方法是什么,这是我的 ChnageNotifier 代码:
class ProfileTracker with ChangeNotifier {
bool _isAuthenticated = false;
static const String AUTH_TRACKER = "AUTH_TRACKER";
void setUp(val)async{
SharedPreferences prefs = await SharedPreferences.getInstance();
_isAuthenticated = await prefs.setBool(AUTH_TRACKER, val);
notifyListeners();
}
bool get isAuthenticated {
return this._isAuthenticated;
}
set isAuthenticated(bool newVal) {
setUp(newVal);
this.notifyListeners();
}
}
那么,这是我登录时设置的代码:
var profileTracker =
Provider.of<ProfileTracker>(context, listen: false);
profileTracker.isAuthenticated = true;
注销时,我使用:
var profileTracker =
Provider.of<ProfileTracker>(context, listen: false);
profileTracker.isAuthenticated = false;
但是当我注销时代码似乎不起作用,状态没有存储在共享首选项中。
【问题讨论】:
标签: flutter sharedpreferences flutter-provider