You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用GoRoute+Riverpod实现认证流程时遇路由配置错误

问题排查:GoRouter与Riverpod集成的断言错误

错误信息

════════ 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'

问题根源

在MaterialApp.router配置中,你同时传入了routerConfig和单独的routeInformationProvider、routeInformationParser、routerDelegate参数。Flutter的断言规则明确禁止这种组合:当提供routerConfig时,不能再单独设置其他路由委托类参数,因为routerConfig已经包含了所有必要的路由组件。

修复方案

修改main.dart中的MaterialApp.router配置,移除多余的路由参数,只保留routerConfig:

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,
      ...
    );
  }
}

可选优化建议

  1. 调整初始路由:当前AppRouting中initialLocation设为首页路径,不符合闪屏启动流程,建议改为:

    initialLocation: APP_PAGE.splash.toPath,
    

    让应用启动直接进入闪屏页,再通过redirect逻辑完成后续跳转。

  2. 修正redirect中的路径生成:state.namedLocation需要传入路由名称(toName)而非路径(toPath),否则会生成错误路径,修改示例:

    final splashLocation = state.namedLocation(APP_PAGE.splash.toName);
    final homeLocation = state.namedLocation(APP_PAGE.home.toName);
    final loginLocation = state.namedLocation(APP_PAGE.login.toName);
    

内容的提问来源于stack exchange,提问作者DarkSide77

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 08:05:08