Flutter路由跳转异常求助:除首页外其他路由均失效
Flutter自定义路由无法跳转问题解决
问题情况
Flutter项目中,除首页外其他自定义路由均无法正常跳转。点击登录按钮时通过以下代码发起跳转:
onSubmitAnimationCompleted: () { Navigator.pushNamed(context, AppRoutes.homeScreen); }
尝试使用AppRoutes.homeScreen常量和直接写'/homeScreen'两种方式,均无法解决问题,出现如下错误日志:
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Could not find a generator for route RouteSettings("/homefeed", null) in the _WidgetsAppState. E/flutter ( 4592): Make sure your root app widget has provided a way to generate E/flutter ( 4592): this route. E/flutter ( 4592): Generators for routes are searched for in the following order: E/flutter ( 4592): 1. For the "/" route, the "home" property, if non-null, is used. E/flutter ( 4592): 2. Otherwise, the "routes" table is used, if it has an entry for the route. E/flutter ( 4592): 3. Otherwise, onGenerateRoute is called. It should return a non-null value for any valid route not handled by "home" and "routes". E/flutter ( 4592): 4. Finally if all else fails onUnknownRoute is called. E/flutter ( 4592): Unfortunately, onUnknownRoute was not set.
现有代码
routes.dart
import 'package:flutter/material.dart'; import 'package:gowild/Screens/homeScreen.dart'; import '../screens/login_screen.dart'; import '../screens/registration_screen.dart'; class AppRoutes { static const String home = '/'; static const String registration = '/registration'; static const String homeScreen = '/homeScreen'; static final Map<String, WidgetBuilder> routes = { AppRoutes.home: (context) => const RegistrationScreen(), AppRoutes.homeScreen: (context) => const HomeScreen(), AppRoutes.registration: (context) => const RegistrationScreen(), }; }
main.dart
import 'package:flutter/material.dart'; import 'login_screen.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { final theme = ThemeData( unselectedWidgetColor: const Color.fromARGB(255, 165, 160, 160), textTheme: const TextTheme( displaySmall: TextStyle( fontFamily: 'OpenSans', fontSize: 45.0, color: Color.fromARGB(255, 255, 255, 255), ), labelLarge: TextStyle( fontFamily: 'OpenSans', fontWeight: FontWeight.bold, fontStyle: FontStyle.italic, color: Colors.white, ), titleMedium: TextStyle( fontFamily: 'NotoSans', color: Colors.white, ), bodyMedium: TextStyle( fontFamily: 'NotoSans', color: Colors.white, ), ), colorScheme: ColorScheme.fromSwatch() .copyWith(secondary: Colors.orange) .copyWith( outline: const Color.fromARGB(255, 13, 158, 61)) //appbar color .copyWith( background: const Color.fromARGB( 255, 39, 38, 38)) //background color dont change .copyWith( primary: const Color.fromARGB(255, 13, 158, 61)) //primary color .copyWith( onPrimary: const Color.fromARGB(255, 254, 255, 255)) //text color .copyWith(onSecondary: const Color.fromARGB(255, 15, 119, 55)), ); return MaterialApp( title: 'GoWild', theme: theme, home: const LoginScreen(), ); } }
解决方法
1. 给MaterialApp配置路由表
你的main.dart中没有将定义好的AppRoutes.routes传递给MaterialApp的routes参数,这是路由无法找到的核心原因。修改MaterialApp部分代码:
return MaterialApp( title: 'GoWild', theme: theme, // 用initialRoute配合路由表的首页配置,避免home属性和路由表冲突 initialRoute: AppRoutes.home, // 添加这一行,关联自定义路由表 routes: AppRoutes.routes, );
如果想保留home: const LoginScreen(),则需要调整routes.dart中的首页映射,避免路由冲突:
static final Map<String, WidgetBuilder> routes = { AppRoutes.home: (context) => const LoginScreen(), // 修改首页映射 AppRoutes.homeScreen: (context) => const HomeScreen(), AppRoutes.registration: (context) => const RegistrationScreen(), };
2. 检查路由名一致性
错误日志中显示找不到的路由是"/homefeed",但你代码中定义的是"/homeScreen",说明项目中存在其他地方误写了路由名。全局搜索"/homefeed",将其替换为正确的AppRoutes.homeScreen或"/homeScreen"。
3. 可选:配置onUnknownRoute(兜底)
为了避免类似错误导致崩溃,可以给MaterialApp添加onUnknownRoute参数,作为路由找不到时的兜底处理:
return MaterialApp( // ...其他参数 onUnknownRoute: (settings) { return MaterialPageRoute( builder: (context) => const LoginScreen(), // 跳转到首页或错误页面 ); }, );
内容的提问来源于stack exchange,提问作者Pasindu Prabhath
相关产品推荐
相关产品推荐

