Named routes help you move between screens easily by giving each screen a simple name. This makes your app navigation clear and organized.
Named routes in Flutter
MaterialApp( initialRoute: '/home', routes: { '/home': (context) => HomeScreen(), '/settings': (context) => SettingsScreen(), }, )
initialRoute sets the first screen shown when the app starts.
routes is a map where keys are route names and values are functions returning the screen widgets.
Navigator.pushNamed(context, '/settings');MaterialApp( initialRoute: '/', routes: { '/': (context) => HomeScreen(), '/profile': (context) => ProfileScreen(), }, )
Navigator.pop(context);
This app starts at the Home screen. Pressing the button moves to the About screen using a named route. The About screen has a button to go back.
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( initialRoute: '/', routes: { '/': (context) => HomeScreen(), '/about': (context) => AboutScreen(), }, ); } } class HomeScreen extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('Home')), body: Center( child: ElevatedButton( onPressed: () { Navigator.pushNamed(context, '/about'); }, child: Text('Go to About'), ), ), ); } } class AboutScreen extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('About')), body: Center( child: ElevatedButton( onPressed: () { Navigator.pop(context); }, child: Text('Back to Home'), ), ), ); } }
Always use unique names for each route to avoid confusion.
You can pass data between screens using arguments with named routes.
Use Navigator.pop(context) to go back to the previous screen.
Named routes give each screen a simple name for easy navigation.
Define routes in MaterialApp with a map of names to screen widgets.
Use Navigator.pushNamed() to move to a screen and Navigator.pop() to go back.
Practice
Solution
Step 1: Understand named routes concept
Named routes assign simple string names to screens for easier navigation instead of using widget instances directly.Step 2: Identify the main purpose
This helps developers navigate between screens by referring to route names, making code cleaner and navigation simpler.Final Answer:
To give each screen a simple name for easy navigation -> Option DQuick Check:
Named routes = simple screen names for navigation [OK]
- Confusing named routes with app theming
- Thinking named routes manage app state
- Mixing named routes with event handling
Solution
Step 1: Recall MaterialApp named routes syntax
Named routes are defined using the 'routes' property with a map of route names to builder functions.Step 2: Check each option
MaterialApp(routes: {'/home': (context) => HomeScreen()}) correctly uses 'routes' with a map where the value is a function returning the widget. Other options use incorrect property names or values.Final Answer:
MaterialApp(routes: {'/home': (context) => HomeScreen()}) -> Option BQuick Check:
Use 'routes' with map of name to builder function [OK]
- Using wrong property names like routeNames or navigation
- Assigning widget instance instead of builder function
- Missing context parameter in builder function
MaterialApp(
routes: {
'/': (context) => HomeScreen(),
'/profile': (context) => ProfileScreen(),
'/settings': (context) => SettingsScreen(),
},
)Solution
Step 1: Check the routes map for '/profile'
The routes map defines '/profile' as a key with ProfileScreen() as its widget builder.Step 2: Understand Navigator.pushNamed behavior
Calling Navigator.pushNamed with '/profile' will navigate to the ProfileScreen widget.Final Answer:
ProfileScreen -> Option AQuick Check:
Navigator.pushNamed('/profile') shows ProfileScreen [OK]
- Confusing route names or mistyping them
- Assuming default screen shows instead of named route
- Expecting error when route exists
Navigator.pushNamed('/settings');Solution
Step 1: Check Navigator.pushNamed method signature
Navigator.pushNamed requires a BuildContext as the first argument, then the route name as a string.Step 2: Identify the error in the code
The code calls Navigator.pushNamed with only the route name string, missing the required context parameter.Final Answer:
Missing BuildContext parameter -> Option CQuick Check:
Navigator.pushNamed needs context first [OK]
- Omitting the BuildContext argument
- Passing route name as first argument without context
- Confusing pushNamed with pop
Solution
Step 1: Recall how to pass arguments with named routes
Navigator.pushNamed accepts an optional 'arguments' parameter to pass data to the new screen.Step 2: Analyze each option
Navigator.pushNamed(context, '/details', arguments: 42); correctly passes context, route name, and arguments. Navigator.pushNamed(context, '/details/42'); tries to encode ID in route string (not standard). Navigator.pushNamed('/details', arguments: 42); misses context. Navigator.pushNamed(context, '/details', params: 42); uses wrong parameter name 'params'.Final Answer:
Navigator.pushNamed(context, '/details', arguments: 42); -> Option AQuick Check:
Use 'arguments' parameter to pass data [OK]
- Omitting context parameter
- Passing data in route string instead of arguments
- Using wrong parameter name like 'params'
Ready to prove it?
Pass the test and this pattern moves unknown
