How to navigate to new page after state change in flutter?

Posted by Editorial Staff | Updated on

How to navigate to new page after state change in flutter?

To answer your direct question, addPostFrameCallback or Future.delayed can call code after a minimal delay. However, there are some issues with this pattern:

1. I would recommend that you only have one MaterialApp widget. You can have a separate route for the login page.

2. In Flutter, state flows from parent to child. Children should not be calling methods on parent State. Instead the parent can pass callbacks to the children. Or perhaps a ChangeNotifier. If there’s no other way to do what you want, you could use a GlobalKey to get access to the AppComponentState from any place in the app and call setCredential on it, but you’re losing some encapsulation and testability by doing so.

3. Pushing another “/” on the navigator stack might cause issues if the user presses the back button and goes back to the previous place. Probably what you want is to trigger a rebuild of some widget that is a parent of LoginPage and Desktop. Or you can call runApp() again to force everything to rebuild.

If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@] Your question and answer will appear on and help other developers.

Related Questions & Answers