Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I need to replace my current screen and found replace method in Navigator's API. But I didn't find any example for it. Maybe somebody knows how to use it.

Thanks.

I am not sure if replace is what you are looking for. You can take a look at the Navigator class . creativecreatorormaybenot Jun 29, 2018 at 13:32 @creativecreatorormaybenot Yep, I looked at this class, and I need to open a new screen without a possibility to close it and return on previous one. Just first screen is a login screen. Ruslan Leshchenko Jun 29, 2018 at 13:36
Route route = MaterialPageRoute(builder: (context) => NextPage());
Navigator.pushReplacement(context, route);

Hope it helps

I have never used Navigator.replace() but this Navigator.pushReplacementNamed might help you.

Navigator.pushReplacementNamed(context, '/thread_list');
                Unfortunately, this method doesn't work. Looks like I should just replace the body of the main widget by setState() method.
– Ruslan Leshchenko
                Jul 2, 2018 at 6:39
                It really works, and for me it was the best solution. Navigator.pushReplacementNamed(context, '/home');
– Alexandre T.
                Apr 5, 2019 at 20:34
                Navigator.pushReplacement replace current router to new. But it's not clear all history. For example Login -> (Add) Register (Replace) -> Home (Click Back button) -> Login
– WiRight
                Dec 23, 2019 at 9:51

If you also want to pop all the routes on the Navigator stack, use

Navigator.pushNamedAndRemoveUntil(context, '/page2', (route) => false);
Navigator.pushAndRemoveUntil(context, 
  MaterialPageRoute(builder: (_) => Page2()), (route) => false);

Here the doc of Navigator.dart

Replaces a route on the navigator that most tightly encloses the given context with a new route. The old route must not be currently visible, as this method skips the animations and therefore the removal would be jarring if it was visible. To replace the top-most route, consider [pushReplacement] instead, which does animate the new route, and delays removing the old route until the new route has finished animating.

@optionalTypeArgs
  static void replace<T extends Object?>(BuildContext context, { required Route<dynamic> oldRoute, required Route<T> newRoute }) {
    return Navigator.of(context).replace<T>(oldRoute: oldRoute, newRoute: newRoute);

replace method is like

Navigator.replace(
    context,
    oldRoute: MaterialPageRoute(builder: (context) => OlRoute()),
    newRoute: MaterialPageRoute(builder: (context) => NewRoute()),
                copy paste a text from the documentation  doesn't count as an answer, please make your next answer more specific and helpful
– abdalmonem
                Mar 6 at 11:09
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.