Flutter : Reset routes

Viewed 21

What should I do to reset routes everytime I call Auth ? I couldn't find the right solution.

For example, if I log in and then log out, if I go back to Auth() I would like the user to be redirected to initialRoute. Currently this only displays the CircularProgressIndicator.

I don't know how to use pushNamedAndRemoveUntil from an other widget to call Auth() widget.

  const Auth({Key? key}) : super(key: key);

  @override
  State<Auth> createState() => _AuthState();
}

class _AuthState extends State<Auth> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(body: Center(child: CircularProgressIndicator()));
  }

  @override
  void initState() {
    super.initState();
    userController();
  }

  @override
  void dispose() {
    super.dispose();

  }

  void userController() {
    runApp(
      MaterialApp(
        initialRoute: '/Login',
        routes: {
          '/Login': (context) => Login(),
          '/LoginPassword': (context) => LoginPassword(),
          '/CodeVerification': (context) => CodeVerification(),
        },
      ),
    );
  }
}

Thanks

2 Answers

This will pop all routes and push the login page.

Navigator.pushNamedAndRemoveUntil(
 context, '/Login', (Route<dynamic> route) => false);

Source : Flutter remove all routes

Try the following code:

Navigator.of(context).pushNamedAndRemoveUntil(
  '/Login',
  (Route<dynamic> route) => false,
);
Related