删除指定时间后的数据。

huangapple go评论113阅读模式
英文:

Delete data after specified time

问题

以下是翻译好的部分:

我有一个Flutter应用程序,在其中我正在使用shared_preferences保存数据。现在我想要实现的是,当数据保存时,它将启动一个5分钟的计时器。5分钟后,数据将被删除,并且用户将被重新带回到登录页。这很简单。但是,我已经完成的部分在每次启动应用程序时都会重新启动计时器。我想要的是,假设在2分30秒后用户关闭了应用程序。一段时间后,用户再次打开应用程序。然后,应用程序将从2分31秒开始,并且剩余时间将为2分29秒。这意味着即使再次打开应用程序,计时器也不会重新启动。

  1. Future<void> initializePreferences() async {
  2. final SharedPreferences prefs = await SharedPreferences.getInstance();
  3. _data = await prefs.getString('myData');
  4. Timer(Duration(minutes: 5), () {
  5. prefs.remove('myData');
  6. setState(() {
  7. _data = null;
  8. });
  9. /// 导航到闪屏页
  10. Navigator.pushNamedAndRemoveUntil(
  11. context,
  12. '/',
  13. (route) => false,
  14. );
  15. print("数据已删除。");
  16. });
  17. }

请注意,这段代码中计时器在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.

  1. Future&lt;void&gt; initializePreferences() async {
  2. final SharedPreferences prefs = await SharedPreferences.getInstance();
  3. _data = await prefs.getString(&#39;myData&#39;);
  4. Timer(Duration(minutes: 5), () {
  5. prefs.remove(&#39;myData&#39;);
  6. setState(() {
  7. _data = null;
  8. });
  9. ///navigate to splashscreen
  10. Navigator.pushNamedAndRemoveUntil(
  11. context,
  12. &#39;/&#39;,
  13. (route) =&gt; false,
  14. );
  15. print(&quot;Data is reremoved.&quot;);
  16. });
  17. }

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 作为计时器方法的参数。

  1. class _MyScreenState extends State<MyScreen> with WidgetsBindingObserver {}
  2. // 在 initState 中添加以下内容:
  3. WidgetsBinding.instance.addObserver(this);
  4. // didChangeAppLifecycleState 将会是:
  5. @override
  6. void didChangeAppLifecycleState(AppLifecycleState state) {
  7. super.didChangeAppLifecycleState(state);
  8. if (state == AppLifecycleState.paused) {
  9. // 进入后台
  10. }
  11. if (state == AppLifecycleState.resumed) {
  12. // 回到前台
  13. }
  14. }
英文:

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

  1. class _MyScreenState extends State&lt;MyScreen&gt; with WidgetsBindingObserver{}
  2. //add the following inside our initState:
  3. WidgetsBinding.instance.addObserver(this);
  4. //didChangeAppLifecycleState will be:
  5. @override
  6. void didChangeAppLifecycleState(AppLifecycleState state) {
  7. super.didChangeAppLifecycleState(state);
  8. if (state == AppLifecycleState.paused) {
  9. // went to Background
  10. }
  11. if (state == AppLifecycleState.resumed) {
  12. // came back to Foreground
  13. }
  14. }

huangapple
  • 本文由 发表于 2023年5月21日 17:16:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76299137.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定