我想每隔24小时删除一次SharedPreferences,即使应用程序关闭也是如此。 这是SharedPreferences:
int _counter = 0;
@override
void initState() {
super.initState();
_loadCounter();
}
//Loading counter value on start
_loadCounter() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
setState(() {
_counter = (prefs.getInt('counter') ?? 0);
});
}
//Incrementing counter after click
_incrementCounter() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
setState(() {
_counter += n;
prefs.setInt('counter', _counter);
});
}
_remove() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
setState(() {
prefs.remove('counter');
});
}
感谢您的帮助!