D
NavigationEasy20 XP2 min read

What is the difference between push, pushReplacement, and pushAndRemoveUntil?

TL;DR: push adds a route to the stack. pushReplacement replaces the current route (back goes to previous). pushAndRemoveUntil pushes a route and removes stack entries until a predicate is satisfied — used for login/logout flows.

Full Answer

  • push: [Home] → [Home, Detail] — back returns to Home
  • pushReplacement: [Home] → [Detail] — replaces Home; no back button
  • pushAndRemoveUntil: [Login, Home, Cart] → [Home] — removes Login and Cart, keeps Home
  • go_router: context.go() ≈ pushReplacement; context.push() ≈ push
🎯

After a successful login, use pushAndRemoveUntil (or context.go('/') in go_router) to clear the auth stack so the user can't back-navigate to the login screen.

Code Examples

dartPost-login stack reset
Output
Navigation stack becomes [HomeScreen] only; Android back button exits the app

Common Mistakes

  • Using push for login success — user can navigate back to login with the Android back button
  • Using (route) => false predicate without understanding it clears the entire stack

Interview Tip

💡

The login flow example is the canonical use case for pushAndRemoveUntil. Every interviewer knows it — nail this answer.

#Navigator#push#pushReplacement#pushAndRemoveUntil#stack