dependencies:
auto_route: [latest-version]
dev_dependencies:
auto_route_generator: [latest-version]
build_runner: [latest-version]
flutter pub get
@MaterialAutoRouter(
replaceInRouteName: 'Page,Route',
routes: <AutoRoute>[
AutoRoute(page: HomePage, initial: true),
AutoRoute(page: BooksPage),
AutoRoute(page: BookDetailsPage),
],
)
class $AppRouter {}
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
final _appRouter = AppRouter()
...
Widget build(BuildContext context){
return MaterialApp.router(
routerDelegate: _appRouter.delegate(),
routeInformationParser: _appRouter.defaultRouteParser(),
),
}