我试图通过路线从一个屏幕导航到另一个屏幕.当我点击页面按钮移动到提供的路线时,我收到错误
I/flutter ( 8790): Another exception was thrown: There are multiple heroes that share the same tag within a subtree.
Run Code Online (Sandbox Code Playgroud)
这是代码:
路线:
<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');
},),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
而错误:
??? 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>
我该如何解决这个问题?
Rob*_*ens 122
之前我遇到过这个问题,因为我在一个屏幕上有两个FloatingAction按钮,我必须为每个FLoatingActionButton添加一个heroTag属性+值,以便错误消失.
例:
new FloatingActionButton(
heroTag: "btn1",
...
)
new FloatingActionButton(
heroTag: "btn2",
...
)
Run Code Online (Sandbox Code Playgroud)
从您提供的示例代码看来,您似乎没有FloatingActionButton,但从错误中它似乎引用它:
I/flutter(21786):在这种情况下,多个英雄有以下标记:默认FloatingActionButton标记
也许您在导航到的页面上使用它然后触发了错误.
无论如何,希望能帮到某个人.
小智 45
你可以设置一个唯一的 id 或者只设置 null:
new FloatingActionButton(
heroTag: null,
...
)
Run Code Online (Sandbox Code Playgroud)
eva*_*als 16
只为未来的读者:
在欣赏到@ m.vincent评论,什么导致这个错误对我来说,我用英雄里面SliverChildBuilderDelegate哪些(显然)有一个索引,所以我用的指标有像标签'tag$index'这样
SliverChildBuilderDelegate(
(BuildContext context, int index) {
return Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Hero(
tag: 'tagImage$index',
child: Image.asset(
'image source here',
),
),
Run Code Online (Sandbox Code Playgroud)
注意:这可能发生在任何具有索引创建子项的小部件上,例如 ListView.Builder
Hei*_*erg 13
还有另一种方法:
FloatingActionButton(
heroTag: UniqueKey(),
....
)
Run Code Online (Sandbox Code Playgroud)
https://api.flutter.dev/flutter/foundation/UniqueKey-class.html
UniqueKey()
创建一个仅等于其自身的密钥。
小智 8
对我来说,仅仅添加一个 uniqueheroTag到 myFloatingActionButtons是行不通的。我最终将其包装heroTag在一个Text小部件中,这为我解决了问题。
new FloatingActionButton(
heroTag: Text("btn1"),
...
)
new FloatingActionButton(
heroTag: Text("btn2"),
...
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13568 次 |
| 最近记录: |