子树中有多个共享相同标签的英雄

编程入门 行业动态 更新时间:2024-10-10 13:14:37
本文介绍了子树中有多个共享相同标签的英雄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试通过路线从一个屏幕导航到另一个屏幕.当我点击页面上的按钮以移动到所提供的路线时,我得到了错误

I am trying to navigate from one screen to another with route. When I hit the button for the page to move to the route provided I get the error

I/flutter ( 8790): Another exception was thrown: There are multiple heroes that share the same tag within a subtree.

代码如下:

路线:

<String, WidgetBuilder>{ '/first':(BuildContext context) =>NavigatorOne() , '/second':(BuildContext context) =>NavigatorTwo(), '/third':(BuildContext context) =>NavigatorThree(), }, Navigator.of(context).pushNamed('/first'); Navigator.of(context).pushNamed('/second'); Navigator.of(context).pushNamed('/third'); class NavigatorOne extends StatefulWidget { @override _NavigatorOneState createState() => _NavigatorOneState(); } class _NavigatorOneState extends State<NavigatorOne> { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(), body: Container( color: Colors.green, child: RaisedButton(child: Text(' one 1'),onPressed: (){ Navigator.of(context).pushNamed('/second'); },), ), ); } }

和错误:

══╡ EXCEPTION CAUGHT BY SCHEDULER LIBRARY ╞═════════════════════════════════════════════════════════ I/flutter (21786): The following assertion was thrown during a scheduler callback: I/flutter (21786): There are multiple heroes that share the same tag within a subtree. I/flutter (21786): Within each subtree for which heroes are to be animated (typically a PageRoute subtree), each Hero I/flutter (21786): must have a unique non-null tag. I/flutter (21786): In this case, multiple heroes had the following tag: <default FloatingActionButton tag>

══╡ EXCEPTION CAUGHT BY SCHEDULER LIBRARY ╞═════════════════════════════════════════════════════════ I/flutter (21786): The following assertion was thrown during a scheduler callback: I/flutter (21786): There are multiple heroes that share the same tag within a subtree. I/flutter (21786): Within each subtree for which heroes are to be animated (typically a PageRoute subtree), each Hero I/flutter (21786): must have a unique non-null tag. I/flutter (21786): In this case, multiple heroes had the following tag: <default FloatingActionButton tag>

我该如何解决?

推荐答案

我以前遇到过这个问题,这是因为我在一个屏幕上有两个FloatingAction按钮,所以我必须为每个以便使错误消失.

I have encountered this before, and it was because I had two FloatingAction buttons on one screen, I had to add a heroTag property + value per FloatingActionButton in order for the error to go away.

示例:

new FloatingActionButton( heroTag: "btn1", ... ) new FloatingActionButton( heroTag: "btn2", ... )

从您提供的示例代码来看,您似乎没有FloatingActionButton,但是从错误看来,它确实引用了它:

From the example code you provided it doesn't appear that you have a FloatingActionButton, but from the error it does seem to reference it:

I/flutter (21786): In this case, multiple heroes had the following tag: default FloatingActionButton tag

也许您在导航页面上使用了它,然后触发了错误.请注意,如果您使用编程方式创建带标签的英雄,则需要找到一种为他们赋予不同标签的方法.例如,如果您有ListView.builder()创建FloatingActionButtons,请尝试以字符串格式传递标签,以便每个按钮都具有不同的标签,例如:heroTag: "btn$index".

Perhaps you used it on the page you were navigating to which then triggered the error. Note that if you're using a programmatic way of creating tagged heroes, you will need to find a way of giving them different tags. For example, if you have a ListView.builder() creating FloatingActionButtons, try passing tags with string formatting so each button has a different tag, e.g.: heroTag: "btn$index".

无论如何,希望能对某人有所帮助.

In any event, hope that helps someone.

更多推荐

子树中有多个共享相同标签的英雄

本文发布于:2023-11-28 08:54:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641748.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:子树   多个   中有   英雄   标签

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!