在 Flutter 中,Getx 包unknownRoute 不起作用

2024-01-19

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

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

我确实喜欢这样:

 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()),
        ],

我有小部件:

class UnknownRoutePage extends StatelessWidget {

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

}

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

 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() 打开。

但是我收到这个消息:

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)
...

为什么这不起作用?

Thanks


我遇到了同样的问题,但我深入研究了 Get 代码,发现有一个matchRoute()对于未知路由,方法将返回“/”。

--> 简而言之,要使unknownRoute工作时,你的初始路径不能是“/”,它可以是其他任何东西,例如“/home”,“/main”,“/init”,但它不能是“/”。


我为它创建了一个 PR:https://github.com/jonataslaw/getx/pull/2256 https://github.com/jonataslaw/getx/pull/2256

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Flutter 中,Getx 包unknownRoute 不起作用 的相关文章

随机推荐

  • 错误:postgresql 中不存在用于更新的列[重复]

    这个问题在这里已经有答案了 我正在尝试将一行文本插入到该列为空的列中 下面列出了错误 任何帮助是极大的赞赏 UPDATE public meditech ar test4 SET filename text WHERE filename i
  • 对 Unity 项目执行版本控制的最佳方法 [关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 我正在开发我的第一个大型 Unity 项目 这是一个游戏 我有很多资产 现在他们使用超过 200 Mbs 的硬盘 我想使用像 GIT 这样的控制
  • 更新到 jruby-1.7.13 后,在 jruby 中要求自制 gem 失败

    我使用了 jruby 1 7 0 和测试框架 Test Unit TestCase 现在我更新了一些gems 导致我使用MiniTest Test进行测试 所以我也升级到了jruby 1 7 13 我也有一个 rake 任务 它使用 jav
  • 打印出 SQL 单个查询 (Yii 1.x)

    我有一个使用 CDbCriteria 生成的大量查询 如下所示 schema Yii app gt db gt schema builder schema gt commandBuilder how to echo out this que
  • 灵活的数组成员,不必是最后一个

    我试图弄清楚 C 中是否有一种解决方法可以在结构中拥有灵活的数组成员 这不是最后一个 例如 这会产生编译错误 typedef struct attribute packed uint8 t slaveAddr lt The slave ad
  • Web API 空白参数值被转换为 null

    我有一个简单的 Web API 服务 大约有 10 种不同的 GET 操作 这些操作根据输入参数返回各种数据库记录的 JSON 输出 对于一个特定端点 一个空格 应该是一个有效的参数 但它被转换为 null 有没有解决的办法 例如网址是 h
  • 是否有返回当前类/方法名称的函数? [复制]

    这个问题在这里已经有答案了 在 C 中 是否有返回当前类 方法名称的函数 当前班级名称 this GetType Name 当前方法名称 using System Reflection MethodBase GetCurrentMethod
  • 找不到gunicorn命令,但它在我的requirements.txt中

    我正在 Heroku 上部署 Django 应用程序 该应用程序已成功部署 但当我想在提供的 http 链接上查看该应用程序时 出现以下错误 gunicorn在我的requirements txt file 2020 02 21T16 22
  • 包含类型的类型的模板函数重载

    我正在尝试执行以下操作 include
  • 在Unity中画出流畅的线条真的那么难吗?

    一段时间以来 我一直在尝试在 Unity 中绘制平滑的线条 但使用线条渲染器时 我只获得了锯齿状的线条 且边角不是圆角的 特别是当曲率角度非常小时 我在质量设置中增加了抗锯齿的值 并尝试了不同的材质 但没有任何改变 我还尝试在每次鼠标移动时
  • 无法保留运行之间的空间

    我想生成一个word文档 作为输入 我有这个字符串 开放包装约定 每个单词都有不同的样式 结果应该是open包装惯例 WordprocessingDocument document WordprocessingDocument Create
  • java属性改变监听器

    我需要处理两个类之间的更改交互 public class HeadClass private Subclass sub public void refresh public class Subclass ArrayList store pu
  • 如何在托管 C++ 中捕获非托管 C++ 异常

    我正在一个大型非托管 C 库和一个大型 C 库上开发一个瘦托管 C 包装器 我需要捕获源自该大型非托管 C 库的错误 并将它们作为 Clr 异常重新抛出 非托管库抛出以下类的实例 Error Error const std string f
  • C# Treeview 移动节点后不刷新

    在我的 Windows 应用程序中 我有一个树视图 我制作了自定义按钮来向下移动节点 单击按钮时会发生以下情况 Node destNode tvCategories SelectedNode NextNode Node srcNode tv
  • 我需要帮助才能在同步模式下进行 FB.api() 调用

    我在 for 循环中运行 FB api 时遇到问题 for var i 0 i lt commentObjectLength i var fbFeedID commentObject OwnCommentList i fbFeedID va
  • 基于机架的 Web 服务器是否代表 FastCGI 协议?

    我读到 CGI FastCGI 是一种用于将外部应用程序连接到 Web 服务器的协议 因此 Web 服务器 如 Apache 或 NginX 通过套接字将环境信息和页面请求本身发送到 FastCGI 进程 并且 FastCGI 通过同一连接
  • ng-repeat 定义的次数而不是在数组上重复的方法?

    有没有办法ng repeat定义的次数而不是总是必须迭代数组 例如 下面我希望列表项显示 5 次 假设 scope number等于 5 并增加数字 因此每个列表项都会增加 1 2 3 4 5 期望的结果 ul li span 1 span
  • 从 Python 快速导入自定义 SPSS 命令

    我编写了一个简洁的 Python 模块 其中包含一些自定义 SPSS 函数供我的同事使用 但是 要使用该功能 他们首先需要输入 BEGIN PROGRAM 在实际调用函数之前导入模块等 因为他们中的大多数人并不精通技术 所以我希望让事情变得
  • TextBox.TextAlign右对齐在某些情况下没有效果?

    我的 Visual C Express 2010 表单应用程序中有一个路径选择器 我用的是FolderBrowserDialog和一个 单行 TextBox 显示所选路径 在我的 UI 刷新代码中使用以下行 this textBoxFold
  • 在 Flutter 中,Getx 包unknownRoute 不起作用

    我开始在 flutter 中学习 Getx 并使用导航 我想设置unknownRoute 以防命名路由中存在拼写错误等 因此应用程序应该转到默认页面 我确实喜欢这样 return GetMaterialApp title Named nav