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

升级go_router至10.1.0后Flutter应用触发断言错误求助

解决go_router升级后routeInformation.state != null断言错误

核心修复步骤

  1. 替换MaterialApp.router的配置方式
    新版本go_router不再推荐单独设置routeInformationParser和routerDelegate,改用统一的routerConfig参数直接传入_router实例:
@override
Widget build(BuildContext context) {
  return MaterialApp.router(
    title: 'my app',
    localizationsDelegates: [
      FFLocalizationsDelegate(),
      GlobalMaterialLocalizations.delegate,
      GlobalWidgetsLocalizations.delegate,
      GlobalCupertinoLocalizations.delegate,
    ],
    locale: _locale,
    supportedLocales: const [Locale('en', '')],
    theme: ThemeData(brightness: Brightness.light),
    darkTheme: ThemeData(brightness: Brightness.dark),
    themeMode: _themeMode,
    // 替换原有的两个参数为以下一行
    routerConfig: _router,
  );
}
  1. 检查GoRouter初始化规范
    确保_router的初始化符合10.x版本要求:
  • 必须明确设置initialLocation,避免启动时路由状态为空;
  • 路由定义使用GoRoute标准结构,示例:
final _router = GoRouter(
  initialLocation: '/',
  routes: [
    GoRoute(
      path: '/',
      builder: (context, state) => HomeScreen(),
    ),
    // 其他路由配置...
  ],
);
  • 确认已完全移除旧版本废弃配置项(如urlPathStrategy)。
  1. 清除缓存重试
    如果上述步骤无效,执行以下操作重置项目环境:
  • 运行flutter clean
  • 重新执行flutter pub get
  • 重启IDE后重新编译项目

内容的提问来源于stack exchange,提问作者JᴀʏMᴇᴇ

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 13:30:28