0

我正在我的颤振应用程序中实现 auto_route 包。我在app_router.dart文件中添加了下面的代码。

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 {}

但是当我运行flutter pub run build_runner build --delete-conflicting-outputs命令时,出现以下错误。

[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

这是我的flutter doctor命令输出。

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.

我正在使用这些依赖项。

dependencies:
  auto_route: ^3.0.4

dev_dependencies:
  auto_route_generator: ^3.0.1
  build_runner: ^2.1.4
4

1 回答 1

0

主要问题在这一行 -

replaceInRouteName: 'Page, Route',

将其替换为 -

replaceInRouteName: 'Page,Route',

解决了这个问题。

于 2021-11-01T09:20:32.783 回答