What is Navigator in Flutter?
Navigator is a widget that manages a stack of Route objects. In simple terms, it's the engine that handles moving between screens (or pages) in your app. When you push a new route, it goes on top of the stack; when you pop, you return to the previous route. Flutter provides a powerful and flexible navigation system that works on iOS, Android, and the web.
Basic Navigation: push and pop
The simplest way to navigate to a new screen is by using Navigator.push() and Navigator.pop(). You wrap the new screen in a MaterialPageRoute which provides a platform‑adaptive transition.
When you call pop, the current screen is removed from the stack and the previous screen becomes visible again.
Named Routes
For larger apps, managing direct widget references can become messy. Named routes allow you to define route names in the MaterialApp and navigate using those names.
This approach centralizes your route definitions and makes navigation more readable.
Passing Data Between Screens
Often you need to send data to the new screen, or receive data back. With named routes, you can pass arguments using the arguments parameter.
Returning Data from a Screen
Sometimes you want a result back from the screen you pushed. You can use Navigator.pop(context, result) and await the push call.
onGenerateRoute and Route Settings
For dynamic route handling (e.g., parsing URLs, handling unknown routes), you can use onGenerateRoute. It gives you full control over the route creation based on the RouteSettings.
Common Mistakes Beginners Make
- Not wrapping the app in a MaterialApp or CupertinoApp: Navigator relies on the context provided by these widgets. Without them,
Navigator.of(context)will fail. - Using context incorrectly:
contextmust be from a widget that is a descendant of the Navigator. Usually, the context from the current screen'sbuildmethod is fine. - Forgetting to handle the back button: On Android, the physical back button automatically pops the route. However, if you want to intercept or disable it, you need
WillPopScope. - Passing arguments without type safety: Using
Mapfor arguments is flexible but error‑prone. Consider using strongly‑typed objects orargumentsas a custom class. - Not awaiting pop when expecting a result: If you need data back, always
awaitthepushcall and handle the case where the user cancels (pop returns null).
Key Points to Remember
- Navigator manages a stack of routes.
pushadds a route,popremoves it. - Use
MaterialPageRoutefor platform‑adaptive transitions. - Named routes (
routesmap) simplify navigation in larger apps. - Pass data using the
argumentsparameter and retrieve it withModalRoute.of(context)!.settings.arguments. - Return data by passing a value to
popand awaiting thepushcall. onGenerateRoutegives you full control for dynamic routes and error handling.- Always ensure your
BuildContextis valid for navigation (usually inside the widget tree of a screen).
Common Interview Questions
- How does Navigator work internally?
- What is the difference between
Navigator.pushandNavigator.pushNamed? - How would you pass data from a second screen back to the first?
- Explain the purpose of
onGenerateRouteand when you would use it instead of theroutesmap. - How can you handle deep links in Flutter using Navigator?
- What is
WillPopScopeand when would you use it?