飘扬的未来。删除计时器在导航到其他页面时处置

发布于 2025-02-12 06:22:15 字数 790 浏览 0 评论 0原文

在我的Flutter应用程序中,我的功能具有延迟5秒以激活按钮。当我导航到其他页面时,即使我使用“ PushDeplacement” Navigator,延迟的计时器仍在工作。任何人都可以帮助我找到一种方法,当我导航到其他页面时,可以处理或取消此计时器。

这是代码:

 Future sendVerificationEmail() async {
  try{
    final user =FirebaseAuth.instance.currentUser!;
    await user.sendEmailVerification();

    setState(() => canResendEmail = false);
    await Future.delayed(const Duration(seconds: 5)); // this is the line causing the error
    setState(() => canResendEmail = true);

  }catch (e) {
    Utils.showSnackBar(e.toString());
  }
  }

这是导航按钮函数:

Future<void> SignOut() async {
   await FirebaseAuth.instance.signOut();
   Navigator.pushReplacement(
     context,MaterialPageRoute(builder: (context) =>  mainPage()),

   );
 }

In my flutter app, I have a function that has delaye for 5 seconds to activate a button. When I navigate to other page, the timer of the delayed is still working even though I use the "pushReplacement" navigator. Can anyone help me find a way to dispose or cancel this timer when I navigate to other page.

here is the code:

 Future sendVerificationEmail() async {
  try{
    final user =FirebaseAuth.instance.currentUser!;
    await user.sendEmailVerification();

    setState(() => canResendEmail = false);
    await Future.delayed(const Duration(seconds: 5)); // this is the line causing the error
    setState(() => canResendEmail = true);

  }catch (e) {
    Utils.showSnackBar(e.toString());
  }
  }

and here is the navigation button function:

Future<void> SignOut() async {
   await FirebaseAuth.instance.signOut();
   Navigator.pushReplacement(
     context,MaterialPageRoute(builder: (context) =>  mainPage()),

   );
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

全部不再 2025-02-19 06:22:18

尝试使用计时器

Timer timer = Timer(Duration(seconds: 5), () {
    //do something here();
  });
  // You can dispose the timer like this
  timer.cancel();

Try using a timer instead

Timer timer = Timer(Duration(seconds: 5), () {
    //do something here();
  });
  // You can dispose the timer like this
  timer.cancel();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文