Flutter auto_router:错误:type' navigatorState'未找到
问题摘要
hi,我有一个问题,我遇到 auto_route
auto_route pocke一个扑朔迷离的项目。在官方文档之后,我完成了以下操作:
- 创建一个用
@materialautorouter
- ran code Generator的占位符类(使用
-Enable-experiment = Enhanced-Enums
) - 挂钩 路由器类
使用
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:
- Created a placeholder class annotated with
@MaterialAutoRouter
- Ran the code generator (using
--enable-experiment=enhanced-enums
) - 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我找到了一个解决方案。这是微不足道的,是由于文档不完整而引起的。浏览
auto_route
github问题页面之后,我发现,从2021年12月1日起。修复是添加
到占位符类(在我的情况下,
app_router.dart
)。因此,编辑(2022年3月16日):正如下面评论中的Yogi Arif Widodo指向点,您可能想导入材料和Cupertino,
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
to the placeholder class (in my case,
app_router.dart
). Therefore,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