在Flutter中,路由不起作用。

huangapple go评论107阅读模式
英文:

Routing in flutter doesn't work correctly

问题

以下是您提供的代码的中文翻译:

  1. Flutter中路由功能不正常
  2. 这是我的`routes.dart`代码:
  3. ```dart
  4. import 'package:flutter/material.dart';
  5. import '../screens/homeScreen.dart';
  6. import '../screens/login_screen.dart';
  7. import '../screens/registration_screen.dart';
  8. class AppRoutes {
  9. static const String home = '/';
  10. static const String registration = '/registration';
  11. static const String homeScreen = '/homeScreen';
  12. static final Map<String, WidgetBuilder> routes = {
  13. AppRoutes.home: (context) => const RegistrationScreen(),
  14. AppRoutes.homeScreen: (context) => const HomeScreen(),
  15. AppRoutes.registration: (context) => const RegistrationScreen(),
  16. };
  17. }

当单击登录按钮时,我调用了如下的pushNamed:

  1. onSubmitAnimationCompleted: () {
  2. Navigator.pushNamed(context, AppRoutes.homeScreen);
  3. }
  1. [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] 未处理的异常:在_WidgetsAppState中找不到路由RouteSettings("/homefeed", null)的生成器。E/flutter ( 4592): 请确保您的根应用程序小部件提供了生成此路由的方法。E/flutter ( 4592): 路由的生成器按以下顺序搜索:E/flutter ( 4592): 1. 对于"/"路由,如果不为空,则使用"home"属性。E/flutter ( 4592): 2. 否则,使用"routes"表,如果它对路由有条目。E/flutter ( 4592): 3. 否则,将调用onGenerateRoute。它应该为任何有效的路由返回非空值,而不由"home""routes"处理。E/flutter ( 4592): 4. 最后,如果一切都失败,则调用onUnknownRouteE/flutter ( 4592): 不幸的是,未设置onUnknownRoute

除了主页路由,我为每个路由都分配了,但是所有其他路由都不起作用,都会出现上述错误。

这是我的main.dart

  1. import 'package:flutter/material.dart';
  2. import 'login_screen.dart';
  3. void main() {
  4. runApp(const MyApp());
  5. }
  6. class MyApp extends StatelessWidget {
  7. const MyApp({Key? key});
  8. @override
  9. Widget build(BuildContext context) {
  10. final theme = ThemeData(
  11. unselectedWidgetColor: const Color.fromARGB(255, 165, 160, 160),
  12. textTheme: const TextTheme(
  13. displaySmall: TextStyle(
  14. fontFamily: 'OpenSans',
  15. fontSize: 45.0,
  16. color: Color.fromARGB(255, 255, 255, 255),
  17. ),
  18. labelLarge: TextStyle(
  19. fontFamily: 'OpenSans',
  20. fontWeight: FontWeight.bold,
  21. fontStyle: FontStyle.italic,
  22. color: Colors.white,
  23. ),
  24. titleMedium: TextStyle(
  25. fontFamily: 'NotoSans',
  26. color: Colors.white,
  27. ),
  28. bodyMedium: TextStyle(
  29. fontFamily: 'NotoSans',
  30. color: Colors.white,
  31. ),
  32. ),
  33. colorScheme: ColorScheme.fromSwatch()
  34. .copyWith(secondary: Colors.orange)
  35. .copyWith(outline: const Color.fromARGB(255, 13, 158, 61))
  36. .copyWith(background: const Color.fromARGB(255, 39, 38, 38))
  37. .copyWith(primary: const Color.fromARGB(255, 13, 158, 61))
  38. .copyWith(onPrimary: const Color.fromARGB(255, 254, 255, 255))
  39. .copyWith(onSecondary: const Color.fromARGB(255, 15, 119, 55)),
  40. );
  41. return MaterialApp(
  42. title: 'GoWild',
  43. theme: theme,
  44. home: const LoginScreen(),
  45. );
  46. }
  47. }
  1. 希望这可以帮助您解决路由问题。如果您有任何其他问题,请随时提出。
  2. <details>
  3. <summary>英文:</summary>
  4. Routes not functioning correctly in Flutter
  5. This is my `routes.dart` code:

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

  1. When the login button is clicked, I called pushNamed as shown below:
  2. `onSubmitAnimationCompleted: () {`
  3. `Navigator.pushNamed(context, AppRoutes.homeScreen);`
  4. `}`

[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 generateE/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.

  1. This happens for every route I assigned except the home route. All other routes are not working and give the above error.
  2. I tried both types, calling `AppRoutes.homeScreen` and `&#39;/homeScreen&#39;`, but nothing works.
  3. here is my `main.dart`
  4. import &#39;package:flutter/material.dart&#39;;
  5. import &#39;login_screen.dart&#39;;
  6. void main() {
  7. runApp(const MyApp());
  8. }
  9. class MyApp extends StatelessWidget {
  10. const MyApp({super.key});
  11. @override
  12. Widget build(BuildContext context) {
  13. final theme = ThemeData(
  14. unselectedWidgetColor: const Color.fromARGB(255, 165, 160, 160),
  15. // brightness: Brightness.dark,
  16. textTheme: const TextTheme(
  17. displaySmall: TextStyle(
  18. fontFamily: &#39;OpenSans&#39;,
  19. fontSize: 45.0,
  20. color: Color.fromARGB(255, 255, 255, 255),
  21. ),
  22. labelLarge: TextStyle(
  23. fontFamily: &#39;OpenSans&#39;,
  24. fontWeight: FontWeight.bold,
  25. fontStyle: FontStyle.italic,
  26. color: Colors.white,
  27. ),
  28. titleMedium: TextStyle(
  29. fontFamily: &#39;NotoSans&#39;,
  30. color: Colors.white,
  31. ),
  32. bodyMedium: TextStyle(
  33. fontFamily: &#39;NotoSans&#39;,
  34. color: Colors.white,
  35. ),
  36. ),
  37. colorScheme: ColorScheme.fromSwatch()
  38. // .copyWith(brightness: )
  39. .copyWith(secondary: Colors.orange)
  40. .copyWith(
  41. outline: const Color.fromARGB(255, 13, 158, 61)) //appbar color
  42. .copyWith(
  43. background: const Color.fromARGB(
  44. 255, 39, 38, 38)) //background color dont change
  45. .copyWith(
  46. primary: const Color.fromARGB(255, 13, 158, 61)) //primary color
  47. .copyWith(
  48. onPrimary: const Color.fromARGB(255, 254, 255, 255)) //text color
  49. .copyWith(onSecondary: const Color.fromARGB(255, 15, 119, 55)),
  50. );
  51. return MaterialApp(
  52. title: &#39;GoWild&#39;,
  53. theme: theme,
  54. home: const LoginScreen(),
  55. );
  56. }
  57. }
  58. </details>
  59. # 答案1
  60. **得分**: 1
  61. Your MaterialApp需要有路由或onGenerator路由输入来识别命名路由。
  62. 返回 MaterialApp(
  63. title: 'GoWild',
  64. theme: theme,
  65. home: const LoginScreen(),
  66. routes: <String, WidgetBuilder>{
  67. AppRoutes.home: (context) => const RegistrationScreen(),
  68. AppRoutes.homeScreen: (context) => const HomeScreen(),
  69. AppRoutes.registration: (context) => const RegistrationScreen(),
  70. },
  71. );
  72. <details>
  73. <summary>英文:</summary>
  74. Your materialApp needs to have routes or onGenerator route input to recognize named routes.
  75. return MaterialApp(
  76. title: &#39;GoWild&#39;,
  77. theme: theme,
  78. home: const LoginScreen(),
  79. routes: &lt;String, WidgetBuilder&gt;{
  80. AppRoutes.home: (context) =&gt; const RegistrationScreen(),
  81. AppRoutes.homeScreen: (context) =&gt; const HomeScreen(),
  82. AppRoutes.registration: (context) =&gt; const RegistrationScreen(),
  83. },
  84. );
  85. </details>

huangapple
  • 本文由 发表于 2023年5月29日 23:58:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76358747.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定