flutter 错误:如果提供了routerConfig,则不能提供所有其他路由器委托(GoRouting包)

mm9b1k5b  于 2023-05-23  发布在  Flutter
关注(0)|答案(1)|浏览(219)

我想用GoRoute和Riverpod创建一个身份验证流。
如下所示:
1.显示启动画面。
1.当显示启动画面时,检查用户是否具有令牌。
1.基于该结果,将用户导航到登录屏幕或主屏幕。
当尝试运行应用程序时,发生了此错误:

════════ Exception caught by widgets library ═══════════════════════════════════
The following assertion was thrown building MaterialApp(dirty, state: _MaterialAppState#8a7c5):
If the routerConfig is provided, all the other router delegates must not be provided
'package:flutter/src/widgets/app.dart':
app.dart:1
Failed assertion: line 454 pos 14: '(routeInformationProvider ?? routeInformationParser ?? routerDelegate ?? backButtonDispatcher) == null'

AppRouting类:

class AppRouting {
  // final appService;
  final Ref ref;

  GoRouter get router => _routes;

  AppRouting({
    required this.ref,
  });

  late final appService = ref.watch(appServiceProvider);
  late final _routes = GoRouter(
    refreshListenable: appService,
    initialLocation: APP_PAGE.home.toPath,
    routes: [
      GoRoute(
        name: APP_PAGE.home.toName,
        path: APP_PAGE.home.toPath,
        // builder: (context, state) => const HomeScreen(),
        builder: (context, state) => const LoginScreen(),
      ),
      GoRoute(
        name: APP_PAGE.login.toName,
        path: APP_PAGE.login.toPath,
        builder: (context, state) => const LoginScreen(),
      ),
      GoRoute(
        name: APP_PAGE.resetPassword.toName,
        path: APP_PAGE.resetPassword.toPath,
        builder: (context, state) => const ResetPasswordScreen(),
      ),
      GoRoute(
        name: APP_PAGE.listView.toName,
        path: APP_PAGE.listView.toPath,
        builder: (context, state) => ListViewScreen(),
      ),
      GoRoute(
        name: APP_PAGE.palettQrCode.toName,
        path: APP_PAGE.palettQrCode.toPath,
        builder: (context, state) => PalettQrCode(),
      ),
      GoRoute(
        name: APP_PAGE.settings.toName,
        path: APP_PAGE.settings.toPath,
        builder: (context, state) => SettingScreen(),
      ),
      // GoRoute(
      //   name: AppRoutingConstans.girdViewRouteName,
      //   path: GridViewScreen.screenPath,
      //   builder: (context, state) => GridViewScreen(),
      // ),
      GoRoute(
        name: APP_PAGE.productPicker.toName,
        path: APP_PAGE.productPicker.toPath,
        builder: (context, state) => ProductPickUpScreen(),
      ),
      GoRoute(
        name: APP_PAGE.error.toName,
        path: APP_PAGE.error.toPath,
        builder: (context, state) => ErrorScreen(
          error: state.extra.toString(),
        ),
      ),
      GoRoute(
        name: APP_PAGE.splash.toName,
        path: APP_PAGE.splash.toPath,
        builder: (context, state) => const SplashScreen(),
      ),
    ],
    errorBuilder: (context, state) => ErrorScreen(
      error: state.error.toString(),
    ),
    redirect: (context, state) {
      final splashLocation = state.namedLocation(APP_PAGE.splash.toPath);
      final homeLocation = state.namedLocation(APP_PAGE.home.toPath);
      final loginLocation = state.namedLocation(APP_PAGE.login.toPath);

      final isLogedIn = appService.loginState;
      final isInitialized = appService.initialized;

      final isGoingToLogin = state.path == loginLocation;
      final isGoingToInit = state.path == splashLocation;

      print(state.path);

      // If not Initialized and not going to Initialized redirect to Splash
      if (!isInitialized && !isGoingToInit) {
        return splashLocation;
        // If not logedin and not going to login redirect to Login
      } else if (isInitialized && !isLogedIn && !isGoingToLogin) {
        return loginLocation;
        // If all the scenarios are cleared but still going to any of that screen redirect to Home
      } else if ((isLogedIn && isGoingToLogin) ||
          (isInitialized && isGoingToInit)) {
        return homeLocation;
      } else {
        // Else Don't do anything
        return null;
      }
    },
  );
}

// GoRouter configuration

final goRouterProvider = Provider<AppRouting>((ref) {
  return AppRouting(ref: ref);
});

在主.dart

class MyApp extends ConsumerStatefulWidget {
  const MyApp({super.key});

  @override
  ConsumerState<ConsumerStatefulWidget> createState() => _MyAppState();
}

class _MyAppState extends ConsumerState<MyApp> {
  @override
  Widget build(BuildContext context) {
    final goRouter = ref.read(goRouterProvider).router;

    return MaterialApp.router(
      ...
      routerConfig: goRouter,
      routeInformationProvider: goRouter.routeInformationProvider,
      routeInformationParser: goRouter.routeInformationParser,
      routerDelegate: goRouter.routerDelegate,
      ...
    );
  }
}
omvjsjqw

omvjsjqw1#

从错误消息:
如果提供了routerConfig,则不能提供所有其他路由器委托。
更改此:

return MaterialApp.router(
  ...
  routerConfig: goRouter, // <== provided
  routeInformationProvider: goRouter.routeInformationProvider, // <== not needed
  routeInformationParser: goRouter.routeInformationParser, // <== not needed
  routerDelegate: goRouter.routerDelegate, // <== not needed
  ...
);

对此:

return MaterialApp.router(
  ...
  routerConfig: goRouter,
  ...
);

相关问题