问题描述
我正在尝试通过路线从一个屏幕导航到另一个屏幕.当我点击页面上的按钮以移动到所提供的路线时,我得到了错误
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 HeroI/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 HeroI/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.
这篇关于子树中有多个共享相同标签的英雄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!