D
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.

AspectNamed RoutesAnonymous Routes
DefinitionCentralized routes map in MaterialAppInline in Navigator.push()
Type safetyString-based โ€” typos cause runtime errorsCompile-time safety with constructors
ParametersVia settings.arguments (untyped)Via constructor (typed)
Deep linkingLimitedNot 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