英文:
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) => 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) => widget,
),
(Route<dynamic> route) => false,
);
});
答案1
得分: 1
你可以声明timer
:
Timer? timer;
用法如下:
timer = Timer(Duration(seconds: 3), () {
Navigator.push(context,
MaterialPageRoute(
builder: (context) => HomeScreen(),
),
);
});
你可以在ontap
或dispose
方法中取消计时器,像这样:
timer.cancel();
英文:
you can declare the timer
Timer? timer;
use like this,
timer = Timer(Duration(seconds: 3), () {
Navigator.push(context,
MaterialPageRoute(
builder: (context) => HomeScreen(),
),
);
});
you can cancel
the timer in the ontap
or dispose
method like this.
timer.cancel();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论