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

时间:2018-07-01 16:21:47

标签: flutter

我正在尝试通过路线从一个屏幕导航到另一个屏幕。当我点击页面上的按钮以移动到所提供的路线时,我得到了错误

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>

我该如何解决?

9 个答案:

答案 0 :(得分:53)

我以前遇到过这种情况,这是因为我在一个屏幕上有两个FloatingAction按钮,所以我必须为每个FLoatingActionButton添加heroTag属性+值,以消除错误。

示例:

new FloatingActionButton(
    heroTag: "btn1",
    ...
)

new FloatingActionButton(
    heroTag: "btn2",
    ...
)

从您提供的示例代码来看,似乎没有FloatingActionButton,但是从错误看来,它确实引用了它:

  

I / flutter(21786):在这种情况下,多个英雄具有以下标签:默认的FloatingActionButton标签

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

无论如何,希望能对某人有所帮助。

答案 1 :(得分:30)

您可以设置唯一ID或仅将其设置为null:

new FloatingActionButton(
heroTag: null,
...
)

答案 2 :(得分:4)

仅供以后阅读:

感谢@ m.vincent的评论,对我造成这个错误的原因是,我在SliverChildBuilderDelegate中使用了Hero(显然)有一个索引,所以我使用了带有{{1 }}

'tag$index'

注意:任何带有索引创建子级的小部件(例如ListView.Builder

)都可能发生这种情况

答案 3 :(得分:3)

对我来说,仅向我的heroTag添加唯一的FloatingActionButtons是行不通的。我最终将heroTag包装在Text小部件中,这为我解决了这个问题。

new FloatingActionButton(
    heroTag: Text("btn1"),
    ...
)

new FloatingActionButton(
    heroTag: Text("btn2"),
    ...
)

答案 4 :(得分:2)

使用英雄标签(如字符串)添加尽可能多的小部件

  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,
      ),
    );
  }

答案 5 :(得分:1)

我遇到了同样的错误。这是因为在单个屏幕中多次使用了诸如浮动操作按钮之类的按钮。
以前,我使用了浮动操作按钮,而是将其更改为手势检测器来使用ontap,这样它就可以了

  GestureDetector(

              //backgroundColor: Colors.amber[100],
              onTap: add,
              child: Icon(
                FontAwesomeIcons.plus,
                size: 30,
              ),
            ),
            SizedBox(
              width: 20,
            ),
            GestureDetector(
             // heroTag: "btn2",
             // backgroundColor: Colors.amber[100],
              onTap: sub,
              child: Icon(FontAwesomeIcons.minus, size: 30),
            ),

答案 6 :(得分:0)

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

答案 7 :(得分:0)

Simple solution of this heroTag issue is to provide this property as unique value. Either it can be a String value. In my case i provided it a String casted index value. And following is my solution which is working perfectly 

    FloatingActionButton(onPressed: (){ 
    print(index); 
    Navigator.push( context, 
MaterialPageRoute( builder: (context)=> AddLead()
     ),
     );
     }, 
    heroTag: '$index',
    child: Image.asset('assets/pencil.png',height: 30, width: 30,), backgroundColor: Constants.myColor, ),

------------------------------------------------------------------------

for more information follow this link https://wise4rmgodadmob.medium.com/how-to-solve-there-are-multiple-heroes-that-share-the-same-tag-within-a-subtree-a5146fdec2b8

答案 8 :(得分:-3)

我遇到了同样的问题,我通过关闭Visual Studio代码然后打开它并运行项目来解决了该问题