Installation and Usage

Edit your pubspec.yaml


dependencies:
  auto_route: [latest-version]

dev_dependencies:
  auto_route_generator: [latest-version]
  build_runner: [latest-version]

Update your packages


flutter pub get

Create your router


@MaterialAutoRouter(
    replaceInRouteName: 'Page,Route',
    routes: <AutoRoute>[
      AutoRoute(page: HomePage, initial: true),
      AutoRoute(page: BooksPage),
      AutoRoute(page: BookDetailsPage),
    ],
)
class $AppRouter {}

Now run the generator


flutter packages pub run build_runner watch

We use the watch flag to watch for file changes which will rebuild the generated files when necessary. If you would like to only generate files once and exit after use:

flutter packages pub run build_runner build

Finally, link the router to your app


final _appRouter = AppRouter()
...
Widget build(BuildContext context){
    return MaterialApp.router(
        routerDelegate: _appRouter.delegate(),
        routeInformationParser: _appRouter.defaultRouteParser(),
    ),
}