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.
–
–
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');
–
–
–
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()),
–
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.