flutter - auto_route build_runner 未构建 [构造函数不能有返回类型]

标签 flutter build-runner

我正在我的 flutter 应用程序中实现 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

最佳答案

主要问题在这一行 -

replaceInRouteName: 'Page, Route',

将其替换为 -

replaceInRouteName: 'Page,Route',

解决了问题。

关于flutter - auto_route build_runner 未构建 [构造函数不能有返回类型],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69794828/

相关文章:

Flutter:读取传递给 build.yaml 文件中的 build_runner 的变量

flutter - 错误 : "type ' UnspecifiedInvalidResult' is not a subtype of type 'LibraryElementResult' in type cast"in Flutter Hive

Flutter 在相机预览和捕获上显示 Logo

android - 您无法推出此版本,因为它不允许任何现有用户升级到 Flutter 应用中新添加的 APK

flutter - 假设小部件的宽度从字符串中间切出文本

flutter - 在 flutter 中单击按钮添加小部件时处理动态复选框列表

flutter - 我需要在 flutter 中更改底部菜单的颜色

flutter - 为什么在 dart/flutter 中序列化 JSON 时 build_runner 不生成文件

dart - 如何为字段注释生成 Dart 代码?