NavigationBeginner10 XP2 min read
What are named routes and when should you prefer them over anonymous routes?
TL;DR: Named routes map string paths to widget builders in MaterialApp.routes. They reduce duplication in large apps but lack type safety. For new apps, go_router is preferred over raw named routes.
Full Answer
Named routes let you navigate by string path rather than constructing a widget inline. They're defined in MaterialApp's routes or onGenerateRoute.
| Aspect | Named Routes | Anonymous Routes |
|---|---|---|
| Definition | Centralized routes map in MaterialApp | Inline in Navigator.push() |
| Type safety | String-based โ typos cause runtime errors | Compile-time safety with constructors |
| Parameters | Via settings.arguments (untyped) | Via constructor (typed) |
| Deep linking | Limited | Not supported natively |
๐ฏ
Both named and anonymous routes are Navigator 1.0 patterns. For any new project, go_router replaces both with type-safe, deep-link-capable routing.
Code Examples
dartNamed routes with argument passing
Output
Navigates to ProductScreen; args['id'] == '42'
Common Mistakes
- โTypos in route strings โ cause runtime 'route not found' errors
- โCasting settings.arguments without checking type โ crash if wrong type is passed
Interview Tip
๐ก
Show awareness that the Flutter team recommends go_router over raw named routes for new projects.
#named-routes#Navigator#routes-map#MaterialApp