【问题标题】:couldn't create named routes in Flutter无法在 Flutter 中创建命名路由
【发布时间】:2022-10-14 06:47:03
【问题描述】:

我在 Material App 中定义了所需的路线,一切正常,我必须添加另一个按钮,这应该会导致授权页面,现在我收到了这个错误:

The following assertion was thrown while handling a gesture:
Could not find a generator for route RouteSettings("/changeUsers", null) in the _WidgetsAppState.

Make sure your root app widget has provided a way to generate 
this route.
Generators for routes are searched for in the following order:
 1. For the "/" route, the "home" property, if non-null, is used.
 2. Otherwise, the "routes" table is used, if it has an entry for the route.
 3. Otherwise, onGenerateRoute is called. It should return a non-null value for any valid route not handled by "home" and "routes".
 4. Finally if all else fails onUnknownRoute is called.

我的代码如下所示:

 return MaterialApp(
      debugShowCheckedModeBanner: false,
      initialRoute: '/',
      routes: {
        '/': (context) => const AuthorizationPage(),
        '/adminLogin': (context) => const AuthInsertLogging(),
        '/mainPageUsers': (context) => const TabBarBottomUsers(),
        '/mainPageAdmin': (context) => const TabBarBottom(),
        "/logout": (_) => new AuthorizationPage(),
        '/changeUsers': (_) => AuthorizationPage(),
      },
    );
  }

这是我试图制作路线的按钮:

 child: ElevatedButton(
                    onPressed: () {
                      Navigator.pushNamed(context, '/changeUsers');
                    },
                    child: Text(
                      'Change User',
                      style: GoogleFonts.montserrat(
                          color: Colors.white,
                          fontSize: 20,
                          fontWeight: FontWeight.w500,
                          letterSpacing: 2),
                    ),

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    这是因为您将AuthorizationPage 用于'/' home, '/logout' and '/changeUsers' routes。有Flutter 文档中的 routes 属性规则.查看详情here

    如果指定了home,则意味着此表中有一个条目用于 Navigator.defaultRouteName 路由(这里是 '/' ),这是一个错误 在路由表中冗余地提供这样的路由。

    规则说必须只指定一个 home 路由(此处为 '/' route )。这里有三个指向同一页面的路由。

    因此,只需 remove '/logout' and '/changeUers' routes 并编写 onPressed 函数如下。

    Navigator.pushNamed(context, '/');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-17
      • 1970-01-01
      • 2021-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-24
      • 2021-04-09
      相关资源
      最近更新 更多