首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在颤动中,Getx包unknownRoute不工作

在颤动中,Getx包unknownRoute不工作
EN

Stack Overflow用户
提问于 2021-09-02 19:20:13
回答 3查看 697关注 0票数 0

我开始学习flutter中的Getx,并使用导航。

我想设置unknownRoute,以防命名路由中有拼写错误等,所以应用程序应该转到默认页面。

我喜欢这样:

代码语言:javascript
复制
 return GetMaterialApp(
        title: 'Named navigation',
        unknownRoute: GetPage(name: '/notfound', page: () => UnknownRoutePage()),
        initialRoute: '/', // this defines with route will be opened first

        getPages: [
          GetPage(name: '/', page: () => MyNavigationNamed()),
          GetPage(name: '/second', page: () => SecondScreenNamed()),
          GetPage(name: '/third', page: () => ThirdParametersScreenNamed()),
          GetPage(
              name: '/third_with_built_param/:someValue',
              page: () => ThirdParametersScreenNamed()),
        ],

我有一个小部件:

代码语言:javascript
复制
class UnknownRoutePage extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Container(child: Text('UNKNOWN ROUTE')));
  }

}

但是,当我尝试通过在路由名称中出现错误来测试它时,如下所示:

代码语言:javascript
复制
 ElevatedButton(
                  onPressed: () {
                    Get.toNamed(
                      '/s');     //THIS IS A DUMMY INCORRECT NAME TO TESTING
                  },
                  child: Text('Error in route name, goes to defalt set above.'),
                ),

我希望打开我的UnknownRoutePage()。

然而,我得到了这样的信息:

代码语言:javascript
复制
The following assertion was thrown building Directionality(textDirection: ltr):
'package:flutter/src/widgets/framework.dart': Failed assertion: line 5033 pos 14: '_dependents.isEmpty': is not true.


Either the assertion indicates an error in the framework itself, or we should provide substantially more information in this error message to help you determine and fix the underlying cause.
In either case, please report this assertion by filing a bug on GitHub:
  https://github.com/flutter/flutter/issues/new?template=2_bug.md

The relevant error-causing widget was: 
  Directionality file:///Users/reuvenberman/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/get-4.3.8/lib/get_navigation/src/root/get_material_app.dart:217:12
When the exception was thrown, this was the stack: 
#2      InheritedElement.debugDeactivated.<anonymous closure> (package:flutter/src/widgets/framework.dart:5033:14)
#3      InheritedElement.debugDeactivated (package:flutter/src/widgets/framework.dart:5035:6)
#4      _InactiveElements._deactivateRecursively.<anonymous closure> (package:flutter/src/widgets/framework.dart:1869:15)
#5      _InactiveElements._deactivateRecursively (package:flutter/src/widgets/framework.dart:1871:6)
#6      ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4628:14)
...
====================================================================================================

======== Exception caught by widgets library =======================================================
The following assertion was thrown while finalizing the widget tree:
Duplicate GlobalKey detected in widget tree.

The following GlobalKey was specified multiple times in the widget tree. This will lead to parts of the widget tree being truncated unexpectedly, because the second time a key is seen, the previous instance is moved to the new location. The key was:
- [LabeledGlobalKey<NavigatorState>#56deb Key Created by default]
This was determined by noticing that after the widget with the above global key was moved out of its previous parent, that previous parent never updated during this frame, meaning that it either did not update at all or updated before the widget was moved, in either case implying that it still thinks that it should have a child with that global key.
The specific parent that did not update after having one or more children forcibly removed due to GlobalKey reparenting is:
- Directionality(textDirection: ltr)
A GlobalKey can only be specified on one widget at a time in the widget tree.
When the exception was thrown, this was the stack: 
#0      BuildOwner.finalizeTree.<anonymous closure> (package:flutter/src/widgets/framework.dart:2900:15)
#1      BuildOwner.finalizeTree (package:flutter/src/widgets/framework.dart:2925:8)
#2      WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:877:19)
#3      RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:328:5)
#4      SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1144:15)
...

为什么这不起作用?

谢谢

EN

回答 3

Stack Overflow用户

发布于 2021-10-05 23:41:17

我遇到了类似的问题,但我没有收到任何错误消息。相反,当我试图导航到一个不存在的页面时,我总是在起始页结束。我没有找到任何关于这一点的澄清。

我的解决方案是为initialRoute设置更重要的内容,例如: initialRoute:'/beginpage‘。'/‘和'/start’对我不起作用,也许它们是一些预留的路线。我在你的代码中看到了同样的问题,也试着改变它。

如果这不起作用,你可以看看我在github上找到的一个现成的例子,它帮助我找出了我的问题:https://github.com/ducafecat/getx_quick_start

票数 1
EN

Stack Overflow用户

发布于 2022-02-13 23:51:51

我也面临着同样的问题,也找不到任何关于这个主题的信息。在做了一些测试并查看了实际的包代码之后,我得出结论,GetX只关心它是否能够匹配URI开头的路由。因此,如果您有一个"/“的路由,那么"/”之后的任何内容,例如“/不存在的路由/1/2/3/”都将与"/“匹配,因为它以"/”开头。类似地,如果您有一个类似于"/admin-area“的路由和另一个用于"/admin-area/home”的路由,那么"/admin-area/non-existent- route /1/2/3/“仍然与"/admin-area”匹配,而像"/admin-area/home/non-existent-route/1/2/3/“这样的URI仍然与"/admin-area/home”匹配。由于这不是web的预期行为,因此在使用currentRoute突出显示导航中的当前页面时可能会出现问题,而且考虑到GetX的文档非常糟糕,我只能假设这是一个错误。

我可以正确解决这个问题的唯一方法是根本不使用GetMaterialApp getPages属性,而是使用onGenerateRoute。Get.toNamed仍然有效,路由器将只路由到完全匹配的项,如下所示:

代码语言:javascript
复制
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      initialRoute: "/",
      onGenerateRoute: (RouteSettings route) {
        var uri = Uri.parse(route.name!);
        switch (uri.path) {
          case "/":
            return MaterialPageRoute(
              settings: route,
              builder: (context) => const RootPage(),
            );
          default:
            return MaterialPageRoute(
              settings: route,
              builder: (context) => const UnkownPage(),
            );
        }
      },
    );
  }

如果您愿意,也可以将此逻辑移出GetMaterialApp,如下所示:

代码语言:javascript
复制
@override
Widget build(BuildContext context) {
  return GetMaterialApp(
    initialRoute: "/",
    onGenerateRoute: generateRoutes
    },
  );
}

//another file
Route<dynamic> generateRoutes(RouteSettings route) {
  var uri = Uri.parse(route.name!);
  switch (uri.path) {
    case "/":
      return MaterialPageRoute(
        settings: route,
        builder: (context) => const UnimplementedPage('Unkown'),
      );
    default:
      return MaterialPageRoute(
        settings: route,
        builder: (context) => const UnimplementedPage('Unkown'),
      );
  }
}
票数 0
EN

Stack Overflow用户

发布于 2022-02-16 11:32:43

我发现了一种简单的方法,可以跳过"getPage:“中的主页,因为实现了"initialRoute:”。

代码语言:javascript
复制
return GetMaterialApp(
    title: 'Named navigation',
    unknownRoute: GetPage(name: '/notfound', page: () => UnknownRoutePage()),
    initialRoute: '/',

    getPages: [

      GetPage(name: '/second', page: () => SecondScreenNamed()),
      GetPage(name: '/third', page: () => ThirdParametersScreenNamed()),
      GetPage(
          name: '/third_with_built_param/:someValue',
          page: () => ThirdParametersScreenNamed()),
    ],

试试看。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69035975

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档