Search code examples
flutterdartmobile-application

How to navigate to home from splash screen in second time in flutter?


class _SplashScreenState extends State<SplashScreen> {
 @override
 void initState() {
 super.initState();
 Timer(
    Duration(seconds: 5),
    () => Navigator.push(
        context, MaterialPageRoute(builder: (context) => Home())));
}

I have timer of 5 seconds to navigate to the Home() page. When i run the app for the very first time it navigate to the home screen but when i back to splash screen again. it loads for a life time.

How can i navigate every time to home screen after 5 seconds?


Solution

  • If you don't need to go back to the splash screen from Home() page.

    Then you may use Navigator.pushReplacement()instead.

    class _SplashScreenState extends State<SplashScreen> {
     @override
     void initState() {
     super.initState();
     Timer(
        Duration(seconds: 5),
        () => Navigator.pushReplacement(
            context, MaterialPageRoute(builder: (context) => Home())));
    }