how can i use popuntil without calling the init state in the ModalRoute.withName and forgot the path?

130

What you need is Navigator.popUntil() method.

In this method you can specify how many screens back you want to go by mentioning the screen to reach.

For Example Let us say you have navigated :

HomeScreen -> Screen1 -> Screen2 -> Screen3

Now you are at screen3 and you want to go to the HomeScreen. So you have to use

Navigator.popUntil(context , ModalRoute.withName("/home-screen") );

(considering "/home-screen" is your route name)

How your routes should be configured in main.dart if it is not already configured like this,

 @override
  Widget build(BuildContext context) {
   return MaterialApp(
          title: 'Flutter app',
          theme: AppTheme.lightTheme,
          darkTheme: AppTheme.darkTheme,
          initialRoute: "/home-screen",
          routes: {
            "/home-screen": (ctx) => Home(),
            "/screen1": (context) => FirstScreen(),
            "/screen2": (context) => SecondScreen(),
            "/screen3": (context) => ThirdScreen(),
          },
        );
    
  }

More important is having a name for your route.

Share:
130
M.Akyuzlu
Author by

M.Akyuzlu

Updated on December 22, 2022

Comments

  • M.Akyuzlu
    M.Akyuzlu over 1 year

    Current screen is 3 pages far from home screen and I want to go back to home screen without recalling the initState on the home screen, after coming back to home screen on pressing back the navigator must not go to the page 3.

    simply i want popAndReplacementNamed but i only have popAndPushNamed