Thuta Learning
Flutter
AdvancedMobile DevelopmentBeginner

Named Routes

FlutterLesson 1925 min

What you'll walk away with

  • Explain the widget, state, and lifecycle behavior of Named Routes
  • Test responsive, failure, and interaction cases
  • Write accessible, testable, and performant Flutter

Explain it simply

Named Routes, in plain language

Named Routes are a navigation approach where you identify screens by a string name. In a project with only a few pages, a direct MaterialPageRoute is simple enough, but once you have more routes, defining them in one central place — like '/profile', '/settings', '/details' — keeps things much cleaner.

Problem → Why → Solution

The problem — why this concept matters

Calling pushNamed with a name that isn't in your routes map will throw a route-not-found error.

A common mistake

Assuming one screen's sample output proves every rebuild, overflow, accessibility, async, and lifecycle path.

Core concept — understand how it works

Build Named Routes as a flow that separates UI, domain logic, data sources, and failure states. Handle loading, empty, success, error, retry, cancellation, and route restoration explicitly.

Mental model
  1. route or gesture
  2. Navigator
  3. screen and result

Explain the widget, state, and lifecycle behavior of Named Routes

Core syntax or pattern

dart
MaterialApp(
  initialRoute: '/',
  routes: {
    '/': (context) => const HomeScreen(),
    '/details': (context) => const DetailScreen(),
    '/settings': (context) => const SettingsScreen(),
  },
);

// Navigate to settings
Navigator.pushNamed(context, '/settings');

// Go back
Navigator.pop(context);

Build Named Routes as a flow that separates UI, domain logic, data sources, and failure states. Handle loading, empty, success, error, retry, cancellation, and route restoration explicitly.

Read the code step by step

  1. route or gestureMaterialApp(

    Named Routes are a navigation approach where you identify screens by a string name. In a project with only a few pages, a direct MaterialPageRoute is simple enough, but once you have more routes, defining them in one central place — like '/profile', '/settings', '/details' — keeps things much cleaner.

  2. Navigator},

    Build Named Routes as a flow that separates UI, domain logic, data sources, and failure states. Handle loading, empty, success, error, retry, cancellation, and route restoration explicitly.

  3. screen and resultNavigator.pop(context);

    Run and test an original Named Routes example with small and large screens, text scaling, empty/loading/error states, rapid interaction, navigation restoration, and disposal cases. Use flutter analyze, widget tests, and profile mode where relevant.

Execution flow
  1. route or gesture
  2. Navigator
  3. screen and result

Success

Explain the widget, state, and lifecycle behavior of Named Routes

Failure

Calling pushNamed with a name that isn't in your routes map will throw a route-not-found error.

Named Routes are a navigation approach where you identify screens by a string name. In a project with only a few pages, a direct MaterialPageRoute is simple enough, but once you have more routes, defining them in one central place — like '/profile', '/settings', '/details' — keeps things much cleaner.

dart
MaterialApp(
  initialRoute: '/',
  routes: {
    '/': (context) => const HomeScreen(),
    '/details': (context) => const DetailScreen(),
    '/settings': (context) => const SettingsScreen(),
  },
);

// Navigate to settings
Navigator.pushNamed(context, '/settings');

// Go back
Navigator.pop(context);
You should see
Calling Navigator.pushNamed(context, '/settings') brings up the SettingsScreen.

Next Steps

Once navigation is done, read on to the Assets lesson to use images and files in your app.

Real-world usage

How to apply it in a real project

Run and test an original Named Routes example with small and large screens, text scaling, empty/loading/error states, rapid interaction, navigation restoration, and disposal cases. Use flutter analyze, widget tests, and profile mode where relevant.

Mini challenge

Build it yourself

Run and test an original Named Routes example with small and large screens, text scaling, empty/loading/error states, rapid interaction, navigation restoration, and disposal cases. Use flutter analyze, widget tests, and profile mode where relevant.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
dart
MaterialApp(
  initialRoute: '/',
  routes: {
    '/': (context) => const HomeScreen(),
    '/details': (context) => const DetailScreen(),
    '/settings': (context) => const SettingsScreen(),
  },
);

// Navigate to settings
Navigator.pushNamed(context, '/settings');

// Go back
Navigator.pop(context);

Expected: Explain the widget, state, and lifecycle behavior of Named Routes

Show hint

Run and test an original Named Routes example with small and large screens, text scaling, empty/loading/error states, rapid interaction, navigation restoration, and disposal cases. Use flutter analyze, widget tests, and profile mode where relevant.

Quick quiz 1 / 3

What best describes the core idea of Named Routes?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • route or gesture → Navigator → screen and result
  • Build Named Routes as a flow that separates UI, domain logic, data sources, and failure states. Handle loading, empty, success, error, retry, cancellation, and route restoration explicitly.
  • Calling pushNamed with a name that isn't in your routes map will throw a route-not-found error.
  • Run and test an original Named Routes example with small and large screens, text scaling, empty/loading/error states, rapid interaction, navigation restoration, and disposal cases. Use flutter analyze, widget tests, and profile mode where relevant.

Flutter documentation — Flutter

Easy traps

  • Calling pushNamed with a name that isn't in your routes map will throw a route-not-found error.
  • Assuming one screen's sample output proves every rebuild, overflow, accessibility, async, and lifecycle path.
  • Doing expensive work in build(), leaking controllers or subscriptions, or calling setState over an unnecessarily large subtree.

Hands-on Exercise

Run and test an original Named Routes example with small and large screens, text scaling, empty/loading/error states, rapid interaction, navigation restoration, and disposal cases. Use flutter analyze, widget tests, and profile mode where relevant.

You'll know it worked when: Calling Navigator.pushNamed(context, '/settings') brings up the SettingsScreen.

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Named Routes | Thuta Learning