Named Routes in Flutter: solved navigation exercise

Named Routes in Flutter: solved exercise

If you are looking for named routes in Flutter, this solved exercise gives you a practical implementation pattern you can reuse in real projects.

Problem statement

Build a screen with:

  • configure two screens
  • navigate from Home to Detail with route names
  • return back with pop

Flutter solution

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    initialRoute: '/',
    routes: {
      '/': (_) => const HomePage(),
      '/detail': (_) => const DetailPage(),
    },
  ));
}

class HomePage extends StatelessWidget {
  const HomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Home')),
      body: Center(
        child: ElevatedButton(
          onPressed: () => Navigator.pushNamed(context, '/detail'),
          child: const Text('Ir a detalle'),
        ),
      ),
    );
  }
}

class DetailPage extends StatelessWidget {
  const DetailPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Detalle')),
      body: Center(
        child: ElevatedButton(
          onPressed: () => Navigator.pop(context),
          child: const Text('Volver'),
        ),
      ),
    );
  }
}

Expected result

Navigation works with a centralized and readable routes definition.

Common mistakes

  • Mixing route styles without a clear rule.
  • Duplicating route names.
  • Ignoring return navigation paths.

Practical use

Useful for apps that need predictable navigation as modules and screens grow.

Guided practice and next step

FAQ

Are named routes enough for most apps?

For many small and medium apps, yes.

Can I pass arguments with named routes?

Yes. Use pushNamed(..., arguments: ...) and read them in the target screen.

Is this still valid in 2026?

Yes. It remains a practical option when simplicity is a priority.