Flutter GoRouter push/go/pushNamed 不起作用

问题描述 投票:0回答:2

我希望在单击按钮时推送页面,并且我正在使用 Flutter 中的 GoRouter 包。 然而,我尝试了各种方法,都没有效果。我尝试使用 Navigator 而不是 GoRouter,它确实有效,但是我想进行深度链接,因此 Navigator 不是一个选项。

这是我的 GoRouter 类

class MyAppRouter{

  GoRouter router = GoRouter(

    routes: [
      GoRoute(
          name: MyAppRouteConstants.defaultRouteName,
          path: '/',
          pageBuilder: (context, state){
            return MaterialPage(child: UnknownPage());
          }
      ),
      GoRoute(
        name: MyAppRouteConstants.registerRouteName,
        path: '/register',
        pageBuilder: (context, state){
          return MaterialPage(child: RegisterPage());
        }
      ),
      GoRoute(
          name: MyAppRouteConstants.loginRouteName,
          path: '/login',
          pageBuilder: (context, state){
            return MaterialPage(child: LogInPage());
          }
      ),
      GoRoute(
          name: MyAppRouteConstants.profileRouteName,
          path: '/profile_page',
          pageBuilder: (context, state){
            return MaterialPage(child: HomePage());
          }
      ),
      GoRoute(
          name: MyAppRouteConstants.addRecipeRouteName,
          path: '/add_recipe',
          pageBuilder: (context, state){
            return MaterialPage(child: CreateActivityPage());
          }
      ),
      GoRoute(
          name: MyAppRouteConstants.publicRecipeRouteName,
          path: '/public_recipes',
          pageBuilder: (context, state){
            return MaterialPage(child: PublicRecipePage());
          }
      ),
      GoRoute(
          name: MyAppRouteConstants.favoritesRouteName,
          path: '/favorites',
          pageBuilder: (context, state){
            return MaterialPage(child: FavoritesPage());
          }
      ),
    ],
    errorPageBuilder: (context, state){
      return MaterialPage(child: UnknownPage());
    }

  );
}

我使用常量作为名称,这些是常量,它们位于同一个文件中

class MyAppRouteConstants{
  static const String defaultRouteName = 'default';
  static const String registerRouteName = 'register';
  static const String loginRouteName = 'login';
  static const String profileRouteName = 'profile_page';
  static const String addRecipeRouteName = 'add_recipe';
  static const String publicRecipeRouteName = 'public_recipes';
  static const String favoritesRouteName = 'favorites';
}

我的主文件如下所示:

  runApp(
    OverlaySupport(
      child: MaterialApp.router(
        debugShowCheckedModeBanner: false,
        theme: new ThemeData(scaffoldBackgroundColor: White_Anti_Flash),
        routeInformationParser: MyAppRouter().router.routeInformationParser,
        routerDelegate: MyAppRouter().router.routerDelegate,
        routeInformationProvider: MyAppRouter().router.routeInformationProvider,
      ),
    ),
  );
}

我将使用的一个示例是当我从 LoginPage 推送 ProfilePage 时

这就是功能

  void logInUser2(String username, String password, BuildContext context) async {

    if (_formKey.currentState!.validate()) {
      bool userExists = await checkUserExists(usernameController.text);

      if(!userExists){
        showOverlayNotification((context) {
          return CompleteMessage(
            message: 'Username does not exist',
          );
        });
      }
      else{
        showOverlayNotification((context) {
          return CompleteMessage(
            message: 'Log In Successful!',
          );
        });
          final user = await loginUser(usernameController.text, passwordController.text);
         if (user != null) {
           print(user.username);
           context.goNamed(MyAppRouteConstants.profileRouteName);

         }
      }
    }
...(more code)

这个函数在这里执行

TextButton(
          onPressed: () async {
             logInUser2(usernameController.text, passwordController.text, context);
         },
),

可以确认线路

context.goNamed(MyAppRouteConstants.profileRouteName);

被执行,我之前已经添加了打印语句。

我尝试使用 go, 和 push,而不是 goNamed,但没有任何效果。我已将该行放在 onPressed: 中,因为函数 logInUser2 是异步的,这可能是一个问题,但这仍然没有解决问题。

我按照文档所述进行了操作,当涉及到导航时,我认为它应该可以工作。

flutter dart flutter-go-router
2个回答
0
投票

MaterialApp.router
里面,你这样通过路由器:

MaterialApp.router(
  // ...
  routeInformationParser: MyAppRouter().router.routeInformationParser,
  routerDelegate: MyAppRouter().router.routerDelegate,
  routeInformationProvider: MyAppRouter().router.routeInformationProvider,
),

注意你如何调用

MyAppRouter
构造函数 3 次,这使得每一次都是该类的不同实例。您可能想这样做:

MaterialApp.router(
  // ...
  routerConfig: MyAppRouter().router,
),

或者如果您可以将实例化分离到变量,那就更好了:

final myAppRouter = MyAppRouter();

并像这样使用它:

MaterialApp.router(
  // ...
  routerConfig: myAppRouter.router,
),

或者如果你只是让路由器成为

MyAppRouter
类的静态成员,那就更好了:

class MyAppRouter {

  static GoRouter router = GoRouter(
    // ...
  );
}

并像这样使用它:

MaterialApp.router(
  // ...
  routerConfig: MyAppRouter.router,
),

另外,请在此处查看 GoRouter 的示例用法:https://github.com/flutter/packages/tree/main/packages/go_router/example/lib


0
投票

在这种情况下使用pushNamed是不合适的,因为它需要GoRouter配置中定义的路由名称。相反,您应该使用 go 或 push,因为这些方法需要位置路径。使用 go 或 push 符合指定路线位置路径的要求,确保正确解释和执行您的导航命令。

© www.soinside.com 2019 - 2024. All rights reserved.