如何在 Flutter 中修复 'AutoRouter operation requested with a context that does not include an AutoRouter.'?
How to fix 'AutoRouter operation requested with a context that does not include an AutoRouter.' in Flutter?
我正在使用 auto_route v2.2.0 包来处理我的 flutter 应用程序中的路由。当我单击按钮转到页面 A/B 时,我得到
AutoRouter operation requested with a context that does not include an AutoRouter.
因为我在 MaterialApp.router 中使用了构建器,所以上下文不应该包含 AutoRouter。我添加了下面的代码。
router.dart
@MaterialAutoRouter(
replaceInRouteName: 'Page,Route',
routes: <AutoRoute>[
AutoRoute(page: MyApp, initial: true),
AutoRoute(page: PageA),
AutoRoute(page: PageB),
],
)
class $AppRouter {}
router.gr.dart
import 'package:auto_route/auto_route.dart' as _i1;
import 'package:flutter/material.dart' as _i2;
import 'main.dart' as _i3;
import 'page_a.dart' as _i4;
import 'page_b.dart' as _i5;
class AppRouter extends _i1.RootStackRouter {
AppRouter([_i2.GlobalKey<_i2.NavigatorState>? navigatorKey])
: super(navigatorKey);
@override
final Map<String, _i1.PageFactory> pagesMap = {
MyApp.name: (routeData) => _i1.MaterialPageX<dynamic>(
routeData: routeData,
builder: (_) {
return _i3.MyApp();
}),
RouteA.name: (routeData) => _i1.MaterialPageX<dynamic>(
routeData: routeData,
builder: (_) {
return _i4.PageA();
}),
RouteB.name: (routeData) => _i1.MaterialPageX<dynamic>(
routeData: routeData,
builder: (_) {
return _i5.PageB();
})
};
@override
List<_i1.RouteConfig> get routes => [
_i1.RouteConfig(MyApp.name, path: '/'),
_i1.RouteConfig(RouteA.name, path: '/page-a'),
_i1.RouteConfig(RouteB.name, path: '/page-b')
];
}
class MyApp extends _i1.PageRouteInfo {
const MyApp() : super(name, path: '/');
static const String name = 'MyApp';
}
class RouteA extends _i1.PageRouteInfo {
const RouteA() : super(name, path: '/page-a');
static const String name = 'RouteA';
}
class RouteB extends _i1.PageRouteInfo {
const RouteB() : super(name, path: '/page-b');
static const String name = 'RouteB';
}
MyApp.dart
class MyApp extends StatelessWidget {
final _appRouter = AppRouter();
@override
Widget build(BuildContext context) {
return MaterialApp.router(
routerDelegate: _appRouter.delegate(),
routeInformationParser: _appRouter.defaultRouteParser(),
builder: (context, widget) {
return Scaffold(
appBar: AppBar(
title: Text('Material App Bar'),
),
body: Center(
child: Column(
children: [
ElevatedButton(
onPressed: () => context.pushRoute(
RouteA()),
child: Text('Page A')),
ElevatedButton(
onPressed: () => context.pushRoute(
RouteA()),
child: Text('Page B'))
],
)),
);
},
);
}
}
此示例是按照提供的文档 here 完成的。
我该如何解决这个问题
将 material 应用程序生成器中的代码删除到您的主页或其他地方。
您收到错误是因为您传递的自定义构建器忽略了传递给构建器的路由器小部件。
我有同样的错误,在我的例子中添加 routerDelegate
和 routeInformationParser
并删除 builder
对我有用。
MaterialApp.router(
routerDelegate: AutoRouterDelegate(
_appRouter,
navigatorObservers: () => [AutoRouteObserver()],
),
routeInformationParser: _appRouter.defaultRouteParser(),
默认路径来自initial: true
.
我正在使用 auto_route v2.2.0 包来处理我的 flutter 应用程序中的路由。当我单击按钮转到页面 A/B 时,我得到
AutoRouter operation requested with a context that does not include an AutoRouter.
因为我在 MaterialApp.router 中使用了构建器,所以上下文不应该包含 AutoRouter。我添加了下面的代码。
router.dart
@MaterialAutoRouter(
replaceInRouteName: 'Page,Route',
routes: <AutoRoute>[
AutoRoute(page: MyApp, initial: true),
AutoRoute(page: PageA),
AutoRoute(page: PageB),
],
)
class $AppRouter {}
router.gr.dart
import 'package:auto_route/auto_route.dart' as _i1;
import 'package:flutter/material.dart' as _i2;
import 'main.dart' as _i3;
import 'page_a.dart' as _i4;
import 'page_b.dart' as _i5;
class AppRouter extends _i1.RootStackRouter {
AppRouter([_i2.GlobalKey<_i2.NavigatorState>? navigatorKey])
: super(navigatorKey);
@override
final Map<String, _i1.PageFactory> pagesMap = {
MyApp.name: (routeData) => _i1.MaterialPageX<dynamic>(
routeData: routeData,
builder: (_) {
return _i3.MyApp();
}),
RouteA.name: (routeData) => _i1.MaterialPageX<dynamic>(
routeData: routeData,
builder: (_) {
return _i4.PageA();
}),
RouteB.name: (routeData) => _i1.MaterialPageX<dynamic>(
routeData: routeData,
builder: (_) {
return _i5.PageB();
})
};
@override
List<_i1.RouteConfig> get routes => [
_i1.RouteConfig(MyApp.name, path: '/'),
_i1.RouteConfig(RouteA.name, path: '/page-a'),
_i1.RouteConfig(RouteB.name, path: '/page-b')
];
}
class MyApp extends _i1.PageRouteInfo {
const MyApp() : super(name, path: '/');
static const String name = 'MyApp';
}
class RouteA extends _i1.PageRouteInfo {
const RouteA() : super(name, path: '/page-a');
static const String name = 'RouteA';
}
class RouteB extends _i1.PageRouteInfo {
const RouteB() : super(name, path: '/page-b');
static const String name = 'RouteB';
}
MyApp.dart
class MyApp extends StatelessWidget {
final _appRouter = AppRouter();
@override
Widget build(BuildContext context) {
return MaterialApp.router(
routerDelegate: _appRouter.delegate(),
routeInformationParser: _appRouter.defaultRouteParser(),
builder: (context, widget) {
return Scaffold(
appBar: AppBar(
title: Text('Material App Bar'),
),
body: Center(
child: Column(
children: [
ElevatedButton(
onPressed: () => context.pushRoute(
RouteA()),
child: Text('Page A')),
ElevatedButton(
onPressed: () => context.pushRoute(
RouteA()),
child: Text('Page B'))
],
)),
);
},
);
}
}
此示例是按照提供的文档 here 完成的。 我该如何解决这个问题
将 material 应用程序生成器中的代码删除到您的主页或其他地方。 您收到错误是因为您传递的自定义构建器忽略了传递给构建器的路由器小部件。
我有同样的错误,在我的例子中添加 routerDelegate
和 routeInformationParser
并删除 builder
对我有用。
MaterialApp.router(
routerDelegate: AutoRouterDelegate(
_appRouter,
navigatorObservers: () => [AutoRouteObserver()],
),
routeInformationParser: _appRouter.defaultRouteParser(),
默认路径来自initial: true
.