0
0
Fluttermobile~20 mins

Named routes in Flutter - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Named Routes Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
navigation
intermediate
2:00remaining
What screen does this named route navigate to?
Given this Flutter code snippet, which screen will be shown when navigating to '/profile'?
Flutter
MaterialApp(
  initialRoute: '/',
  routes: {
    '/': (context) => HomeScreen(),
    '/profile': (context) => ProfileScreen(),
    '/settings': (context) => SettingsScreen(),
  },
);

Navigator.pushNamed(context, '/profile');
AProfileScreen
BHomeScreen
CSettingsScreen
DError screen because '/profile' is not defined
Attempts:
2 left
💡 Hint
Check the routes map for the key '/profile'.
ui_behavior
intermediate
2:00remaining
What happens if you navigate to an undefined named route?
Consider this Flutter app with routes defined only for '/' and '/home'. What happens if you call Navigator.pushNamed(context, '/about')?
Flutter
MaterialApp(
  initialRoute: '/',
  routes: {
    '/': (context) => SplashScreen(),
    '/home': (context) => HomeScreen(),
  },
);

Navigator.pushNamed(context, '/about');
AApp navigates to SplashScreen
BApp navigates to HomeScreen
CApp silently ignores the navigation request
DApp throws a runtime error: Could not find a generator for route "/about"
Attempts:
2 left
💡 Hint
Check what happens when a route is missing in the routes map.
lifecycle
advanced
2:00remaining
Which lifecycle method is called when navigating to a new named route?
When you navigate from ScreenA to ScreenB using Navigator.pushNamed, which lifecycle method of ScreenB's stateful widget is called first?
Adispose()
Bbuild()
CinitState()
DdidChangeDependencies()
Attempts:
2 left
💡 Hint
Think about widget creation lifecycle before build.
📝 Syntax
advanced
2:00remaining
Identify the correct syntax for defining named routes in MaterialApp
Which option correctly defines named routes in a Flutter MaterialApp widget?
Flutter
MaterialApp(
  initialRoute: '/',
  routes: {
    '/': (context) => HomeScreen(),
    '/details': (context) => DetailsScreen(),
  },
);
Aroutes: ['/': HomeScreen(), '/details': DetailsScreen()]
Broutes: {'/': (context) => HomeScreen(), '/details': (context) => DetailsScreen()}
Croutes: ['/': (context) => HomeScreen(), '/details': (context) => DetailsScreen()]
Droutes: {'/': HomeScreen, '/details': DetailsScreen}
Attempts:
2 left
💡 Hint
routes expects a Map.
🔧 Debug
expert
2:00remaining
Why does this named route navigation fail at runtime?
Given this code, why does Navigator.pushNamed(context, '/profile') cause a runtime error?
Flutter
MaterialApp(
  initialRoute: '/',
  routes: {
    '/': (context) => HomeScreen(),
    '/profile': ProfileScreen(),
  },
);

Navigator.pushNamed(context, '/profile');
ABecause '/profile' value should be a function returning a widget, not a widget instance
BBecause '/profile' is missing from routes map
CBecause initialRoute is set to '/' instead of '/profile'
DBecause Navigator.pushNamed requires a BuildContext from MaterialApp
Attempts:
2 left
💡 Hint
Check the type of values in the routes map.