Search code examples
flutterbuild-runner

auto_route build_runner not building [Constructors can't have a return type]


I am implementing auto_route package inside my flutter app. I have added the code below, inside the app_router.dart file.

import 'package:auto_route/annotations.dart';
import 'package:auto_route/auto_route.dart';
import 'package:news_app/pages/home_page.dart';
import 'package:news_app/router/transition_route_builders.dart';

part 'app_router.gr.dart';

@MaterialAutoRouter(
  replaceInRouteName: 'Page, Route',
  routes: <AutoRoute>[
    RedirectRoute(path: '/home-page', redirectTo: '/'),
    CustomRoute(
      page: HomePage,
      initial: true,
      customRouteBuilder: sharedAxisZTransitionRouteBuilder,
    ),
  ],
)
class AppRouter extends _$AppRouter {}

But when I am running flutter pub run build_runner build --delete-conflicting-outputs command, the following error is occurring.

[SEVERE] auto_route_generator:autoRouteGenerator on lib/router/app_router.dart:

Could not format because the source could not be parsed:

line 5, column 151 of .: Expected to find ','.
  ╷
5 │ @override List<RouteConfig> get routes => [RouteConfig('/home-page#redirect', path: '/home-page', redirectTo: '/', fullMatch: true), RouteConfig(Home Route.name, path: '/')];
  │                                                                                                                                                       ^^^^^
  ╵
line 3, column 58 of .: Expected to find ','.
  ╷
3 │ @override final Map<String,PageFactory> pagesMap = {Home Route.name: (routeData) { return  CustomPage<dynamic>(routeData: routeData, child: const HomePage(), customRouteBuilder: sharedAxisZTransitionRouteBuilder, opaque: true, barrierDismissible: false); } };
  │                                                          ^^^^^
  ╵
line 8, column 12 of .: Unexpected text 'Route'.
  ╷
8 │ class Home Route extends PageRouteInfo<void> {const Home Route() : super(name, path: '/');
  │            ^^^^^
  ╵
line 8, column 53 of .: Constructors can't have a return type.
  ╷
8 │ class Home Route extends PageRouteInfo<void> {const Home Route() : super(name, path: '/');
  │                                                     ^^^^
  ╵
line 8, column 58 of .: The name of a constructor must match the name of the enclosing class.
  ╷
8 │ class Home Route extends PageRouteInfo<void> {const Home Route() : super(name, path: '/');
  │                                                          ^^^^^
  ╵
[INFO] Running build completed, took 13.9s

[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 51ms

[SEVERE] Failed after 14.0s
pub finished with exit code 1

Here's my flutter doctor command output.

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 2.5.1, on Microsoft Windows [Version 10.0.22000.282], locale en-IN)
[√] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
[X] Chrome - develop for the web (Cannot find Chrome executable at .\Google\Chrome\Application\chrome.exe)
    ! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.
[√] Visual Studio - develop for Windows (Visual Studio Community 2019 16.8.3)
[√] Android Studio (version 4.1)
[√] VS Code (version 1.61.2)
[√] Connected device (3 available)

! Doctor found issues in 1 category.

I am using these dependencies.

dependencies:
  auto_route: ^3.0.4

dev_dependencies:
  auto_route_generator: ^3.0.1
  build_runner: ^2.1.4

Solution

  • The main issue is in this line -

    replaceInRouteName: 'Page, Route',
    

    replacing it with -

    replaceInRouteName: 'Page,Route',
    

    fixes the issue.