2

我正在使用auto_route v2.2.0 包来处理我的颤振应用程序中的路由。当我单击按钮转到页面 A/B 时,我得到

AutoRouter operation requested with a context that does not include an AutoRouter.

由于我在 MaterialApp.router 中使用了构建器,因此上下文不应该包含 AutoRouter。我在下面添加了代码。

路由器.dart

@MaterialAutoRouter(  
  replaceInRouteName: 'Page,Route',  
  routes: <AutoRoute>[  
    AutoRoute(page: MyApp, initial: true),  
    AutoRoute(page: PageA),  
    AutoRoute(page: PageB),  

  ],  
)  
class $AppRouter {} 

路由器.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'))
            ],
          )),
        );
      },
    );
  }
}

此示例是按照此处提供的文档完成的。我该如何解决这个问题

4

2 回答 2

1

将材料应用程序构建器中的代码删除到您的主页或其他地方。您收到错误是因为您传递了一个自定义构建器,该构建器忽略了传递给构建器的路由器小部件。

于 2021-05-14T13:13:59.397 回答
0

我有同样的错误,在我的情况下,添加routerDelegaterouteInformationParser删除builder对我有用。

MaterialApp.router(
        routerDelegate: AutoRouterDelegate(
          _appRouter,
          navigatorObservers: () => [AutoRouteObserver()],
        ),
        routeInformationParser: _appRouter.defaultRouteParser(),

默认路径来自initial: true.

于 2021-05-27T23:10:34.623 回答