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

Nor*_*ert 50 flutter

我试图通过路线从一个屏幕导航到另一个屏幕.当我点击页面按钮移动到提供的路线时,我收到错误

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标记

也许您在导航到的页面上使用它然后触发了错误.

无论如何,希望能帮到某个人.

  • 你可以简单地取消它: heroTag: null, (16认同)
  • 类似的问题可能在里面带有“英雄”的“ ListTile”中。这个“英雄”不只是一个英雄,而是乘以你的“ items.length”,因此标签应该是“'tag $ index'” (6认同)
  • 哇!我正在导航到黑屏。当我在屏幕上放置自动播放声音文件进行测试时,我知道屏幕正在正确加载。终于注意到这个“英雄”错误消息并找到了您的回复 - 谢谢!确实非常有帮助。 (3认同)
  • 谢谢帮忙! (2认同)

小智 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)

  • 这实在是太奇怪了。 (2认同)