Migration to Flutter Guide
Discover our battle-tested 21-step framework for a smooth and successful migration to Flutter!
Home
Glossary

AutoRoute in Flutter

What is AutoRoute in Flutter?

AutoRoute is a declarative routing library for Flutter that provides type-safe navigation through code generation. Instead of navigating with string-based route names, you define routes once and let AutoRoute generate strongly typed APIs.

This approach significantly reduces runtime navigation errors and improves refactoring safety.

Why does it matter in Flutter app development?

Navigation complexity grows quickly in real apps. AutoRoute helps by:

  • Enforcing type safety for routes and parameters.
  • Centralizing navigation configuration.
  • Making deep linking and web routing easier.
  • Improving maintainability in larger codebases.

It is especially useful when building apps with many screens or URL-based navigation.

How does AutoRoute work?

AutoRoute uses annotations and build_runner to generate routing code. You define your routes in a router configuration file, then run the generator.

A minimal setup looks like this:

import 'package:auto_route/auto_route.dart';

part 'app_router.gr.dart';

@AutoRouterConfig()
class AppRouter extends $AppRouter {
  @override
  List<AutoRoute> get routes => [
    AutoRoute(page: HomeRoute.page, initial: true),
    AutoRoute(page: DetailsRoute.page),
  ];
}

The part 'app_router.gr.dart'; line is required. Without it, the generated $AppRouter class will not be found and the code will not compile.

After defining routes, generate code with:

dart run build_runner build

(or watch during development).

You can then navigate like this:

context.router.push(DetailsRoute(id: 42));

Route arguments are checked at compile time.

Key characteristics of AutoRoute

AutoRoute is based on:

  • Code generation (build_runner)
  • Navigator 2.0 (Router API)
  • Strongly typed routes
  • Declarative configuration

It avoids common issues found in Navigator.pushNamed.

When to use AutoRoute?

Use AutoRoute when:

  • Your app has multiple screens with parameters.
  • You target Flutter Web.
  • You want compile-time safety.
  • You need structured, scalable navigation.
  • You plan to use tab-based navigation.

AutoRoute also provides AutoTabsScaffold, a powerful widget for bottom navigation that preserves tab state automatically.

When not to use AutoRoute?

Avoid AutoRoute if:

  • Your app has very simple navigation.
  • You want to avoid code generation.
  • You are building a small prototype or demo.

In such cases, basic Navigator APIs may be sufficient.

Common mistakes to avoid

A frequent error is forgetting to run build_runner after adding or changing routes. Hot Reload does not regenerate routing code.

Another mistake is mixing AutoRoute navigation with manual Navigator.push calls, which can lead to inconsistent behavior.

Best practices when using AutoRoute

  • Keep all routes defined in one router file.
  • Run the generator whenever routes change.
  • Use route guards for authentication flows instead of handling logic inside widgets.

Alternatives to AutoRoute

GoRouter is the most popular alternative. It offers declarative routing without code generation, but with less compile-time safety.

AutoRoute is a strong choice when correctness, structure, and scalability are priorities.

Learn more

Flutter architecture by LeanCode

Feature-Based Flutter App Architecture - LeanCode

Read about the LeanCode approach to Flutter architecture. We highlight some of the design decisions that should be made when developing a feature in mobile apps. This includes our approach to dependency injection, state management, widget lifecycle, and data fetching.

A replacement for freezed in Dart

No Macros in Dart, How to Replace Freezed?

Unfortunately, the Dart team has taken up the difficult decision of abandoning their work on the macros language feature. Although freezed is a powerful package, it comes with some costs. At some point, we decided to move forward and use alternative solutions for new code and projects. See our approach.

Sharing Logic Across Multiple Platforms - Solution by LeanCode

Sharing Logic Across Multiple Platforms

When a project grows, sooner or later, a need for sharing logic across multiple clients and a server arises. At LeanCode, we completed a project that extensively employed logic sharing successfully. In this article, we highlight the steps and architecture decisions of the picked solution.