在Flutter中,路由不起作用。

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

Routing in flutter doesn't work correctly

问题

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

Flutter中路由功能不正常

这是我的`routes.dart`代码:

```dart
import 'package:flutter/material.dart';
import '../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(),
  };
}

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

onSubmitAnimationCompleted: () {
  Navigator.pushNamed(context, AppRoutes.homeScreen);
}
[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. 最后,如果一切都失败,则调用onUnknownRoute。E/flutter ( 4592): 不幸的是,未设置onUnknownRoute。

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

这是我的main.dart

import 'package:flutter/material.dart';
import 'login_screen.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? 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))
          .copyWith(background: const Color.fromARGB(255, 39, 38, 38))
          .copyWith(primary: const Color.fromARGB(255, 13, 158, 61))
          .copyWith(onPrimary: const Color.fromARGB(255, 254, 255, 255))
          .copyWith(onSecondary: const Color.fromARGB(255, 15, 119, 55)),
    );

    return MaterialApp(
      title: 'GoWild',
      theme: theme,
      home: const LoginScreen(),
    );
  }
}

希望这可以帮助您解决路由问题。如果您有任何其他问题,请随时提出。

<details>
<summary>英文:</summary>

Routes not functioning correctly in Flutter

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


When the login button is clicked, I called pushNamed as shown below:

`onSubmitAnimationCompleted: () {`
`Navigator.pushNamed(context, AppRoutes.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 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.


This happens for every route I assigned except the home route. All other routes are not working and give the above error.

I tried both types, calling `AppRoutes.homeScreen` and `&#39;/homeScreen&#39;`, but nothing works.

here is my `main.dart` 

    import &#39;package:flutter/material.dart&#39;;
    import &#39;login_screen.dart&#39;;
    
    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),
          // brightness: Brightness.dark,
          textTheme: const TextTheme(
            displaySmall: TextStyle(
              fontFamily: &#39;OpenSans&#39;,
              fontSize: 45.0,
              color: Color.fromARGB(255, 255, 255, 255),
            ),
            labelLarge: TextStyle(
              fontFamily: &#39;OpenSans&#39;,
              fontWeight: FontWeight.bold,
              fontStyle: FontStyle.italic,
              color: Colors.white,
            ),
            titleMedium: TextStyle(
              fontFamily: &#39;NotoSans&#39;,
              color: Colors.white,
            ),
            bodyMedium: TextStyle(
              fontFamily: &#39;NotoSans&#39;,
              color: Colors.white,
            ),
          ),
          colorScheme: ColorScheme.fromSwatch()
              // .copyWith(brightness: )
              .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: &#39;GoWild&#39;,
          theme: theme,
          home: const LoginScreen(),
        );
      }
    }



</details>


# 答案1
**得分**: 1

Your MaterialApp需要有路由或onGenerator路由输入来识别命名路由。

返回 MaterialApp(
  title: 'GoWild',
  theme: theme,
  home: const LoginScreen(),
  routes: <String, WidgetBuilder>{
    AppRoutes.home: (context) => const RegistrationScreen(),
    AppRoutes.homeScreen: (context) => const HomeScreen(),
    AppRoutes.registration: (context) => const RegistrationScreen(),
  },
);

<details>
<summary>英文:</summary>

Your materialApp needs to have routes or onGenerator route input to recognize named routes.
    
    return MaterialApp(
      title: &#39;GoWild&#39;,
      theme: theme,
      home: const LoginScreen(),
      routes: &lt;String, WidgetBuilder&gt;{
        AppRoutes.home: (context) =&gt; const RegistrationScreen(),
        AppRoutes.homeScreen: (context) =&gt; const HomeScreen(),
        AppRoutes.registration: (context) =&gt; const RegistrationScreen(),
      },
    );

</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:

确定