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>

我该如何解决这个问题?

最佳答案

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

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

https://stackoverflow.com/questions/51125024/

相关文章:

functional-programming - Dart:映射列表(list.map)

visual-studio-code - 如何为 Flutter 模拟器的 Visual Studi

angular - NativeScript 与 Flutter

dart - Flutter:小部件构建上的运行方法完成

flutter - 如何在 Flutter 中设置按钮的宽度和高度?

flutter - 如何根据父级的大小布局小部件?

flutter - 如何获取 Widget 的高度?

flutter - 从 StatefulWidget 外部控制状态

Flutter 在极端情况下对齐两项 - 一项在左侧,一项在右侧

user-interface - 如何在 Flutter 中垂直和水平居中文本?