升级go_router至10.1.0后Flutter应用触发断言错误求助
解决go_router升级后
routeInformation.state != null断言错误 核心修复步骤
- 替换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, ); }
- 检查GoRouter初始化规范
确保_router的初始化符合10.x版本要求:
- 必须明确设置
initialLocation,避免启动时路由状态为空; - 路由定义使用
GoRoute标准结构,示例:
final _router = GoRouter( initialLocation: '/', routes: [ GoRoute( path: '/', builder: (context, state) => HomeScreen(), ), // 其他路由配置... ], );
- 确认已完全移除旧版本废弃配置项(如
urlPathStrategy)。
- 清除缓存重试
如果上述步骤无效,执行以下操作重置项目环境:
- 运行
flutter clean - 重新执行
flutter pub get - 重启IDE后重新编译项目
内容的提问来源于stack exchange,提问作者JᴀʏMᴇᴇ
相关产品推荐
相关产品推荐

