0

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

Milaethe
  • 43
  • 5

2 Answers2

0

This will pop all routes and push the login page.

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

Source : Flutter remove all routes

Eng
  • 1,617
  • 2
  • 12
  • 25
  • Thx for your help but I call Auth from an other Widget, so how should i do? Because it doesn't work: onPressed: () { Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder: (context) => Auth()), (Route route) => false);}), – Milaethe Sep 24 '22 at 23:41
  • Explain it doesn't work. Your new route is well pushed right ? Previous routes are not removed ? – Eng Sep 25 '22 at 15:27
0

Try the following code:

Navigator.of(context).pushNamedAndRemoveUntil(
  '/Login',
  (Route<dynamic> route) => false,
);
My Car
  • 4,198
  • 5
  • 17
  • 50
  • Thx for your help but I call Auth from an other Widget, so how should i do? Because it doesn't work: onPressed: () { Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder: (context) => Auth()), (Route route) => false);}), – Milaethe Sep 24 '22 at 23:41
  • @Milaethe, can you share the code of main.dart? – My Car Sep 25 '22 at 00:30