gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 08:14:44 28 4
gpt4 key购买 nike

我正在我的 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com