How to prevent going back to previous page on Flutter Web via browser back button?

Issue

After a user successfully logged in in my Flutter web app, it should be taken to a dashboard view.

The user then should not be able to navigate back to the login screen by pressing the browser back button.

I tried a lot of things, replacing the route, popping the route and then pushing the next route, all did not work.

Also using WillPopScope in the dashboard page does not work. Pressing the browser back button does not fire onWillPop.

Found some similar questions, but they all end up suggesting WillPopScope or replacing the route, both did not work for me.

Any idea what I could do?

Solution

Seems like you can’t intercept the browser back buttons.

I solved the problem by using a RouteGuard of the auto_route package which intercepts the navigation and checks if the user is authenticated or not. If the user is already authenticated, the navigation to the Signin page is canceled.

Check the auto_route package for examples and further explanation: https://pub.dev/packages/auto_route#route-guards

Answered By – tmaihoff

Answer Checked By – Marie Seifert (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *