Flutter计时器小部件在导航到另一个屏幕后仍在播放。

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

Flutter Timer widget still playing after navigating to another screen

问题

In my app, I have a custom splash screen that I set as the home page in the MaterialApp. Inside the main widget's build method of the splash screen, there's a Timer, and after 3 seconds, the Timer callback function navigates to the home widget.

Timer(Duration(seconds: 3), () {
  Navigator.push(
    context,
    MaterialPageRoute(
      builder: (context) => HomeScreen(),
    ),
  );
});

The issue is that when I work on the app and perform a hot reload, the hot reload runs successfully, and the changes appear. However, after three seconds, it redirects me to the HomeScreen() even when I'm not working on the splash screen anymore.

I need a solution as this could affect the app's performance. How can I stop the splash screen logic after the Timer finishes the first time?

Note: I'm using BLoC cubit and BlocConsumer on the screen, and there are no dispose() functions that I can use.

I tried to call Navigator.pushAndRemoveUntil(), but it shows me this error: "Unhandled Exception: Null check operator used on a null value."

Timer(Duration(seconds: 3), () {
  Navigator.pushAndRemoveUntil(
    context,
    MaterialPageRoute(
      builder: (context) => widget,
    ),
    (Route<dynamic> route) => false,
  );
});

Is there anything else you'd like to know or clarify about this code?

英文:

in my app I have custom splash screen, I made it the home page in the MaterialApp and there is a Timer inside the build method of the screen main widget and after 3 seconds the Timer callback function navigates the home widget

Timer(Duration(seconds: 3), () {
  Navigator.push(
    context,
    MaterialPageRoute(
      builder: (context) =&gt; HomeScreen(),
    ),
  );
});

The problem is that when I am working on the app and perform any hot reload, the hot reload runs successfully and the changes appears, but after three seconds it redirects me to the HomeScreen(), it feels like the Timer widget is running after any hot reload even when I am not working on the splash screen anymore

I need a solution, I thing the will affect the performance of the app
how to stop the splash screen logic after the Timer finishes the first time?

Note: I am using BLoC cubit and the BlocConsuming on the screen and there is no dispose() functions - I can't use them

I tried to call the Navigator.pushAndRemoveUntil() but it shows me this error Unhandled Exception: Null check operator used on a null value

Timer(Duration(seconds: 3), () {
  Navigator.pushAndRemoveUntil(
    context,
    MaterialPageRoute(
      builder: (context) =&gt; widget,
    ),
    (Route&lt;dynamic&gt; route) =&gt; false,
  );
});

答案1

得分: 1

你可以声明timer

Timer? timer;

用法如下:

timer = Timer(Duration(seconds: 3), () {
    Navigator.push(context,
      MaterialPageRoute(
        builder: (context) => HomeScreen(),
      ),
    );
});

你可以在ontapdispose方法中取消计时器,像这样:

timer.cancel();
英文:

you can declare the timer

Timer? timer;

use like this,

timer = Timer(Duration(seconds: 3), () {
        Navigator.push(context,
          MaterialPageRoute(
           builder: (context) =&gt; HomeScreen(),
            ),
          );
       });

you can cancel the timer in the ontap or dispose method like this.

timer.cancel();

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

发表评论

匿名网友

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

确定