我试图导航从一个屏幕到另一个与路线。当我点击按钮页面移动到路由提供我得到错误

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>

我怎么解决这个问题?


当前回答

仅供未来读者参考:

非常感谢@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',
              ),
            ),

注意:这可能发生在任何带有索引创建子条目的小部件上,比如ListView。构建器

其他回答

如果有多个具有相同标记的项目,则只需设置tag: Uuid()。

你可以设置一个唯一的id或只设置null:

FloatingActionButton(
  heroTag: null,
  ...
)

仅供未来读者参考:

非常感谢@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',
              ),
            ),

注意:这可能发生在任何带有索引创建子条目的小部件上,比如ListView。构建器

添加任意数量的小部件,使用英雄标签作为字符串

  Widget floatingButt(Function function, IconData icon, String heroTag) {
    return FloatingActionButton(
      onPressed: function,
      heroTag: heroTag,
      materialTapTargetSize: MaterialTapTargetSize.padded,
      backgroundColor: Constants.primaryColor, // from your values file
      child: Icon(
        icon,
        size: 36.0,
      ),
    );
  }

我以前遇到过这种情况,这是因为我在一个屏幕上有两个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"。