Where do you define named routes?
The routes property of MaterialApp maps route names to widget builders.
Organize navigation with named routes
Passing the Quiz & Exercises is mandatory to earn FX and proceed. If you fail, try again to gain the required FX, then the Proceed button will become available.
You can read this lesson publicly. Sign in to unlock exercises, quiz, and FX progress tracking.
Having trouble? AI is here to help
Ask context-aware questions about this lesson. Free plan: 5 questions/day. Upgrade to Premium for higher daily limits.
Use the AI button at the bottom of the screen
For larger apps, define all routes in one place using named routes. This makes navigation cleaner and more maintainable.
1// Define routes in MaterialApp2MaterialApp(3initialRoute: '/',4routes: {5'/': (context) => HomeScreen(),6'/profile': (context) => ProfileScreen(),7'/settings': (context) => SettingsScreen(),8'/product': (context) => ProductScreen(),9},10);1112// Navigate using route names13Navigator.pushNamed(context, '/profile');1415// Navigate and replace16Navigator.pushReplacementNamed(context, '/home');1718// Clear stack and navigate19Navigator.pushNamedAndRemoveUntil(20context,21'/login',22(route) => false, // Remove all routes23);
As your app grows, route strings can get scattered across many files. A common pattern is to gather all route names into a single class as static constants. This prevents typos, makes refactoring easier, and gives you one place to see every screen in your app.
1// Create a routes class for organization2class AppRoutes {3static const home = '/';4static const profile = '/profile';5static const settings = '/settings';6static const productDetail = '/product';78static Map<String, WidgetBuilder> get routes => {9home: (context) => HomeScreen(),10profile: (context) => ProfileScreen(),11settings: (context) => SettingsScreen(),12productDetail: (context) => ProductScreen(),13};14}1516// Use in MaterialApp17MaterialApp(18initialRoute: AppRoutes.home,19routes: AppRoutes.routes,20);2122// Navigate with constants23Navigator.pushNamed(context, AppRoutes.profile);
Named routes are great for learning navigation concepts. For production apps, consider the `go_router` package which supports deep linking, typed parameters, and web URLs.
Treat route names as app-level contracts:
Centralized contracts reduce navigation regressions when teams scale and features expand.
While classic named routes (routes: {'/home': (context) => HomeScreen()}) work for simple demos, the Flutter team strongly recommends declarative routing (GoRouter) for production apps. Classic named routes do not synchronize URLs on Flutter Web, break when deep linking complex query parameters, and lose navigation history when switching bottom navigation tabs.
Organize navigation with named routes. Navigation manages how users move between screens and return to previous states.
Connect each concept to real app features you can build.
Design route names and arguments before wiring screen UI.
Poor routing decisions create confusing UX and fragile deep-link behavior.
The routes property of MaterialApp maps route names to widget builders.
pushNamed() navigates using the route name defined in MaterialApp.
When the predicate returns false for all routes, it removes every route and pushes the new one (common for logout → login).
initialRoute specifies which named route to show when the app starts.
Something seems incorrect, unclear, or missing? Let us know.
FlutterCraft editorial lessons are reviewed by experienced Flutter developers and updated to stay aligned with modern Flutter and Dart best practices.
Reference docs: Flutter Documentation, Dart Language Tour, and official package documentation where relevant.
Sign in to continue with exercises and quiz for this lesson.
Sign In to Continue