Flutter auto_router:错误:type' navigatorState'未找到

发布于 2025-01-28 16:05:05 字数 4230 浏览 2 评论 0原文

问题摘要

hi,我有一个问题,我遇到 auto_route auto_route pocke一个扑朔迷离的项目。在官方文档之后,我完成了以下操作:

  1. 创建一个用@materialautorouter
  2. ran code Generator的占位符类(使用-Enable-experiment = Enhanced-Enums
  3. 挂钩 路由器类

使用

lib/app_router.gr.dart:16:26: Error: Type 'NavigatorState' not found.
  _$AppRouter([GlobalKey<NavigatorState>? navigatorKey]) : super(navigatorKey);
                         ^^^^^^^^^^^^^^
lib/app_router.gr.dart:16:16: Error: Type 'GlobalKey' not found.
  _$AppRouter([GlobalKey<NavigatorState>? navigatorKey]) : super(navigatorKey);
               ^^^^^^^^^
lib/app_router.gr.dart:16:26: Error: 'NavigatorState' isn't a type.
  _$AppRouter([GlobalKey<NavigatorState>? navigatorKey]) : super(navigatorKey);
                         ^^^^^^^^^^^^^^
lib/app_router.gr.dart:16:16: Error: 'GlobalKey' isn't a type.
  _$AppRouter([GlobalKey<NavigatorState>? navigatorKey]) : super(navigatorKey);

上升

材料App - version :

Flutter 3.0.0 • channel stable • https://github.com/flutter/flutter.git
Framework • revision ee4e09cce0 (3 days ago) • 2022-05-09 16:45:18 -0700
Engine • revision d1b9a6938a
Tools • Dart 2.17.0 • DevTools 2.12.2

pubspec.yaml的摘录,具有相关依赖关系

environment:
  sdk: ">=2.17.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter
  auto_route: ^4.0.1
 
dev_dependencies:
  auto_route_generator: ^4.0.0
  build_runner: ^2.1.10

代码&amp;其他尝试

(1)占位符类(app_router.dart)用@materialautorouter ...

part 'app_router.gr.dart';

const authRoutes = AutoRoute(
  name: 'AuthRouter',
  path: '/',
  initial: true,
  page: LoginScreen,
);

@MaterialAutoRouter(
  routes: <AutoRoute>[authRoutes],
)
class AppRouter extends _$AppRouter {}

...和(2)flutter flutter pub Rum的自动化代码 ... ... build_runner build -enable-Experiment = Enhated-eNums -delete-conflicting-unputs 命令(app_router.gr.dart

// **************************************************************************
// AutoRouteGenerator
// **************************************************************************

// GENERATED CODE - DO NOT MODIFY BY HAND

// **************************************************************************
// AutoRouteGenerator
// **************************************************************************
//
// ignore_for_file: type=lint

part of 'app_router.dart';

class _$AppRouter extends RootStackRouter {
  _$AppRouter([GlobalKey<NavigatorState>? navigatorKey]) : super(navigatorKey);

  @override
  final Map<String, PageFactory> pagesMap = {
    AuthRouter.name: (routeData) {
      return MaterialPageX<dynamic>(
          routeData: routeData, child: const LoginScreen());
    }
  };

  @override
  List<RouteConfig> get routes => [RouteConfig(AuthRouter.name, path: '/')];
}

/// generated route for
/// [LoginScreen]
class AuthRouter extends PageRouteInfo<void> {
  const AuthRouter() : super(AuthRouter.name, path: '/');

  static const String name = 'AuthRouter';
}

最后,(3),这就是我将其连接起来的方式使用MiteralApp

class App extends StatelessWidget {
  final AppRouter _router = AppRouter();

  App({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      routerDelegate: _router.delegate(),
      routeInformationParser: _router.defaultRouteParser(),
      debugShowCheckedModeBanner: false,
      theme: AppTheme.lightThemeData(context),
    );
  }
}

我昨天迁移到了Flutter 3,同时也开始使用增强的枚举。如果没有-Enable-envermiment = Enhangated-Enums标志,代码生成器似乎不起作用。我尝试使用有或没有相同标志的应用程序(flutter Run)运行应用程序(flutter Run),但是上述错误仍然存​​在。

如果有人对可能出错的事情有任何想法,那么对任何帮助都将受到极大的赞赏。谢谢。

Problem summary

Hi, I have an issue getting the auto_route package up and running in a Flutter project. Following the official documentation, I have done the following:

  1. Created a placeholder class annotated with @MaterialAutoRouter
  2. Ran the code generator (using --enable-experiment=enhanced-enums)
  3. Hook up the router class with MaterialApp

Once I attempt to run the app with flutter run, this is the error message I'm given:

lib/app_router.gr.dart:16:26: Error: Type 'NavigatorState' not found.
  _$AppRouter([GlobalKey<NavigatorState>? navigatorKey]) : super(navigatorKey);
                         ^^^^^^^^^^^^^^
lib/app_router.gr.dart:16:16: Error: Type 'GlobalKey' not found.
  _$AppRouter([GlobalKey<NavigatorState>? navigatorKey]) : super(navigatorKey);
               ^^^^^^^^^
lib/app_router.gr.dart:16:26: Error: 'NavigatorState' isn't a type.
  _$AppRouter([GlobalKey<NavigatorState>? navigatorKey]) : super(navigatorKey);
                         ^^^^^^^^^^^^^^
lib/app_router.gr.dart:16:16: Error: 'GlobalKey' isn't a type.
  _$AppRouter([GlobalKey<NavigatorState>? navigatorKey]) : super(navigatorKey);

Additional information

The output of flutter --version:

Flutter 3.0.0 • channel stable • https://github.com/flutter/flutter.git
Framework • revision ee4e09cce0 (3 days ago) • 2022-05-09 16:45:18 -0700
Engine • revision d1b9a6938a
Tools • Dart 2.17.0 • DevTools 2.12.2

Excerpt of pubspec.yaml file with relevant dependencies

environment:
  sdk: ">=2.17.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter
  auto_route: ^4.0.1
 
dev_dependencies:
  auto_route_generator: ^4.0.0
  build_runner: ^2.1.10

Code & additional attempts

(1) Placeholder class (app_router.dart) annotated with @MaterialAutoRouter...

part 'app_router.gr.dart';

const authRoutes = AutoRoute(
  name: 'AuthRouter',
  path: '/',
  initial: true,
  page: LoginScreen,
);

@MaterialAutoRouter(
  routes: <AutoRoute>[authRoutes],
)
class AppRouter extends _$AppRouter {}

...and the autogenerated code of (2) flutter pub run build_runner build --enable-experiment=enhanced-enums --delete-conflicting-outputs command (app_router.gr.dart)

// **************************************************************************
// AutoRouteGenerator
// **************************************************************************

// GENERATED CODE - DO NOT MODIFY BY HAND

// **************************************************************************
// AutoRouteGenerator
// **************************************************************************
//
// ignore_for_file: type=lint

part of 'app_router.dart';

class _$AppRouter extends RootStackRouter {
  _$AppRouter([GlobalKey<NavigatorState>? navigatorKey]) : super(navigatorKey);

  @override
  final Map<String, PageFactory> pagesMap = {
    AuthRouter.name: (routeData) {
      return MaterialPageX<dynamic>(
          routeData: routeData, child: const LoginScreen());
    }
  };

  @override
  List<RouteConfig> get routes => [RouteConfig(AuthRouter.name, path: '/')];
}

/// generated route for
/// [LoginScreen]
class AuthRouter extends PageRouteInfo<void> {
  const AuthRouter() : super(AuthRouter.name, path: '/');

  static const String name = 'AuthRouter';
}

Lastly, (3), this is how I hooked it up with the MaterialApp:

class App extends StatelessWidget {
  final AppRouter _router = AppRouter();

  App({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      routerDelegate: _router.delegate(),
      routeInformationParser: _router.defaultRouteParser(),
      debugShowCheckedModeBanner: false,
      theme: AppTheme.lightThemeData(context),
    );
  }
}

I have migrated to the Flutter 3 yesterday, and simultaneously started using enhanced enums as well. Without the --enable-experiment=enhanced-enums flag, the code generator doesn't seem to work. I have tried running the app (flutter run) with and without the same flag, but the aforementioned error persists.

If someone has any idea on what could have potentially gone wrong, any help is greatly appreciated. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

揽月 2025-02-04 16:05:05

好的,我找到了一个解决方案。这是微不足道的,是由于文档不完整而引起的。浏览auto_route github问题页面之后,我发现,从2021年12月1日起。

修复是添加

import 'package:flutter/material.dart';

到占位符类(在我的情况下,app_router.dart)。因此,

// + other relevant imports
import 'package:flutter/material.dart';

part 'app_router.gr.dart';

const authRoutes = AutoRoute(
  name: 'AuthRouter',
  path: '/',
  initial: true,
  page: LoginScreen,
);

@MaterialAutoRouter(
  routes: <AutoRoute>[authRoutes],
)
class AppRouter extends _$AppRouter {}

编辑(2022年3月16日):正如下面评论中的Yogi Arif Widodo指向点,您可能想导入材料和Cupertino,

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';

OK, I have found a solution. It's trivial and caused by incomplete documentation. After browsing the auto_route GitHub issues page, I found this, from December 1st, 2021.

The fix is to add

import 'package:flutter/material.dart';

to the placeholder class (in my case, app_router.dart). Therefore,

// + other relevant imports
import 'package:flutter/material.dart';

part 'app_router.gr.dart';

const authRoutes = AutoRoute(
  name: 'AuthRouter',
  path: '/',
  initial: true,
  page: LoginScreen,
);

@MaterialAutoRouter(
  routes: <AutoRoute>[authRoutes],
)
class AppRouter extends _$AppRouter {}

Edit (March 16, 2022): as Yogi Arif Widodo pointed point in the comment below, you might wanna import both Material and Cupertino, should you need it

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文