英文:
Delete data after specified time
问题
以下是翻译好的部分:
我有一个Flutter应用程序,在其中我正在使用shared_preferences保存数据。现在我想要实现的是,当数据保存时,它将启动一个5分钟的计时器。5分钟后,数据将被删除,并且用户将被重新带回到登录页。这很简单。但是,我已经完成的部分在每次启动应用程序时都会重新启动计时器。我想要的是,假设在2分30秒后用户关闭了应用程序。一段时间后,用户再次打开应用程序。然后,应用程序将从2分31秒开始,并且剩余时间将为2分29秒。这意味着即使再次打开应用程序,计时器也不会重新启动。
Future<void> initializePreferences() async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
_data = await prefs.getString('myData');
Timer(Duration(minutes: 5), () {
prefs.remove('myData');
setState(() {
_data = null;
});
/// 导航到闪屏页
Navigator.pushNamedAndRemoveUntil(
context,
'/',
(route) => false,
);
print("数据已删除。");
});
}
请注意,这段代码中计时器在5分钟内启动。您希望实现的剩余部分已在上文中描述。提前谢谢!
英文:
I have a flutter application. Where I am saving a data in shared_preferences. Now what I want to achieve is that when data is saved it will start a timer for 5 minutes. After 5 minutes the data will be removed and user will be taken to the LoginPage again. This is quite simple. But here the things that I have done again restarts the timer whenever I start the application. I want is that suppose after 2 mins 30 seconds user closes the app. After some time the user again opens the app. Then the app will start from 2 mins 31 seconds and remaining time will be 2 mins 29 seconds. That means the timer will not restart even when the app is opened again.
Future<void> initializePreferences() async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
_data = await prefs.getString('myData');
Timer(Duration(minutes: 5), () {
prefs.remove('myData');
setState(() {
_data = null;
});
///navigate to splashscreen
Navigator.pushNamedAndRemoveUntil(
context,
'/',
(route) => false,
);
print("Data is reremoved.");
});
}
here the timer starts for 5 mins. The remaining details what I want to achieve is given above. Thanks in advance.
答案1
得分: 1
你可以在应用从前台(例如:用户正在使用应用)到后台(用户退出应用但尚未终止)时监听应用状态。
在监听应用状态后,您可以添加另一个 shared_prefs 来存储用户暂停应用时的剩余时间,并在应用恢复时,使用先前的 shared_prefs 作为计时器方法的参数。
class _MyScreenState extends State<MyScreen> with WidgetsBindingObserver {}
// 在 initState 中添加以下内容:
WidgetsBinding.instance.addObserver(this);
// didChangeAppLifecycleState 将会是:
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
if (state == AppLifecycleState.paused) {
// 进入后台
}
if (state == AppLifecycleState.resumed) {
// 回到前台
}
}
英文:
You can listen the app state when app is from foreground (ex: user using the app) to background (user quit the app but not terminated yet).
After listen to app state, you could add another shared_prefs to store the time remain when user pause app, and when app in resume, using shared_prefs previously as argument for the timer method
class _MyScreenState extends State<MyScreen> with WidgetsBindingObserver{}
//add the following inside our initState:
WidgetsBinding.instance.addObserver(this);
//didChangeAppLifecycleState will be:
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
if (state == AppLifecycleState.paused) {
// went to Background
}
if (state == AppLifecycleState.resumed) {
// came back to Foreground
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论