flutter 如何使用pushReplacment方法使用路由器包?

wi3ka0sx  于 2023-03-13  发布在  Flutter
关注(0)|答案(2)|浏览(167)

我们如何使用pushReplacement使用go-router包
我试过了,但是没有用

context.pushReplacement("register");

要将导航器API用于命名路由(pushNamed、pushReplacementNamed或pushNamedAndRemoveUntil),必须为导航器提供onGenerateRoute处理程序。
这是我的设置

final GoRouter _router = GoRouter(
    debugLogDiagnostics: true,

  routes: <RouteBase>[
    GoRoute(
      path: '/',
      builder: (BuildContext context, GoRouterState state) {
        return const MainPage();
      },
      routes: <RouteBase>[
        GoRoute(
          path: 'login',
          builder: (BuildContext context, GoRouterState state) {
            return const Login();
          },
        ),
        GoRoute(
          path: 'register',
          builder: (BuildContext context, GoRouterState state) {
            return const SignUp();
          },
        ),
        
      ],
    ),
  ],
);

我想在Flutter中导航。

ffscu2ro

ffscu2ro1#

您必须命名GoRouter路径,然后使用pushReplacementNamed

GoRoute(
          path: 'register',
          name : 'register', //👈 add Name
          builder: (BuildContext context, GoRouterState state) {
            return const SignUp();
          },
),

并将其用作

context.pushReplacementNamed("register");
y53ybaqx

y53ybaqx2#

您应该在路由名称前加上/

context.pushReplacement("/register");

相关问题