行中相同的 Flutter Widgets 抛出错误?

Identical Flutter Widgets in Row throwing error?

我在一行中生成了 2 个 FloatingActionButton。路由到其文件时出现以下错误...

The following assertion was thrown during a scheduler callback: There are multiple heroes that share the same tag within a subtree. Within each subtree for which heroes are to be animated (typically a PageRoute subtree), each Hero must have a unique non-null tag. In this case, multiple heroes had the tag "Instance of 'Object'".

这是我的代码...

new Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                new FloatingActionButton(
                    child: new Icon(Icons.remove), onPressed: _decline),
                new Padding(padding: new EdgeInsets.all(10.0)),
                new Text(
                  _count.toString(),
                  style: new TextStyle(
                      fontSize: 40.0,
                      fontWeight: FontWeight.bold,
                      color: Colors.black),
                ),
                new Padding(padding: new EdgeInsets.all(10.0)),
                new FloatingActionButton(
                    child: new Icon(Icons.add), onPressed: _increment),
              ],
            )

这就是我路由到我的文件的方式...

Navigator.push(context, new MaterialPageRoute(builder: (_) => new Video.VideoPage()));

当我注释掉第一个 FloatingActionButton 时,它工作正常。只有当它们都被使用时才会出错。如果重要的话,我的 Row 也是 Column 小部件的子项。

尝试为每个 FloatingActionButton 添加一个唯一的 heroTag,这样 Flutter 就不会将两个按钮相互混淆,例如:

new Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          new FloatingActionButton(
              heroTag: "Decline",
              child: new Icon(Icons.remove), onPressed: _decline),
          new Padding(padding: new EdgeInsets.all(10.0)),
          new Text(
            _count.toString(),
            style: new TextStyle(
                fontSize: 40.0,
                fontWeight: FontWeight.bold,
                color: Colors.black),
          ),
          new Padding(padding: new EdgeInsets.all(10.0)),
          new FloatingActionButton(
              heroTag: "Increment",
              child: new Icon(Icons.add), onPressed: _increment),
        ],
      ),

我们不能在同一页面上使用多个浮动操作按钮,因此有两种可能性。

  1. 不要使用多个浮动操作按钮(Flutter 很棒 所以还有其他方法可以得到相同的结果)
  2. 不要让 heroTag 为空。