我正在尝试使用路线从一个屏幕导航到另一个屏幕。当我点击页面按钮以移动到提供的路线时,我得到了错误
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>
我该如何解决这个问题?
最佳答案
我以前遇到过这种情况,这是因为我在一个屏幕上有两个 FloatingAction
按钮,我必须为每个 FloatingActionButton
添加一个 heroTag 属性 + 值,以便错误消失。
例子:
FloatingActionButton(
heroTag: "btn1",
...
)
FloatingActionButton(
heroTag: "btn2",
...
)
从您提供的示例代码来看,您似乎没有 FloatingActionButton
,但从错误来看,它似乎确实引用了它:
I/flutter (21786): In this case, multiple heroes had the following tag: default FloatingActionButton tag
也许您在导航到的页面上使用了它,然后触发了错误。请注意,如果您使用编程方式创建标记英雄,您将需要找到一种方法为他们提供不同的标记。例如,如果您有一个 ListView.builder()
创建 FloatingActionButtons
,请尝试使用字符串格式传递标签,以便每个按钮都有不同的标签,例如:heroTag: "btn$index"
.
无论如何,希望对某人有所帮助。
https://stackoverflow.com/questions/51125024/