一尘不染

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

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>

我该如何解决?


阅读 359

收藏
2020-08-13

共1个答案

一尘不染

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

例:

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

new 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"

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

2020-08-13