Flutter踩坑记: There are multiple heroes that share the same tag within a subtree.

Posted wgb1234

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Flutter踩坑记: There are multiple heroes that share the same tag within a subtree.相关的知识,希望对你有一定的参考价值。

There are multiple heroes that share the same tag within a subtree.

Flutter首页里加了脚手架里的加号按钮floatingActionButton,页面结构大致是这样,从上往下依次是appBar,ListView,floatActionButton..,在listView的item里面的一个点击回调的事件里做页面跳转,一跳转就是黑屏,这个问题折腾了许久,主要花在了排查问题上,排查了路由设置问题和类名以及页面结构等问题之后还是无果,最后拿控制台的一句话"There are multiple heroes that share the same tag within a subtree."粘贴到google上出来了一些结果,问题根源在于FloatingActionButton的一个属性标识即heroTag没有设置,要手动设置一个标识唯一即可


Provide<MyCounter>(
      builder: (context, child, val) {
        int currentCount = Provide.value<MyCounter>(context).currentCount;
        return Scaffold(
          appBar: AppBar(
            title: Text("首页 $currentCount", style: TextStyle(color: Colors.white)),
            flexibleSpace: Container(
              decoration: BoxDecoration(
                gradient: LinearGradient(
                    colors: [Colors.blue[100], Colors.blue[300], Colors.blue],
                    begin: Alignment.topCenter,
                    end: Alignment.bottomCenter),
              ),
            ),
          ),
          body: Container(
              child: ListView.builder(
            itemCount: dataList.length,
            itemBuilder: (BuildContext context, int index) {
              String name = dataList[index].name;
              Function callBack = dataList[index].tapCallBack;
              return Column(
                children: <Widget>[
                  ListTile(
                    title: Text(name),
                    onTap: callBack,
                  ),
                  Divider(),
                ],
              );
            },
          )),
          floatingActionButton: FloatingActionButton(
            heroTag: "你大爷的", //就是这个玩意儿
            child: Icon(Icons.add, color: Colors.white, size: 44),
            backgroundColor: Colors.blueAccent,
            onPressed: () {
              Provide.value<MyCounter>(context).updateCount(currentCount);
            },
          ),
        );
      },
    );

参考博文
Flutter: There are multiple heroes that share the same tag within a subtree异常
flutter 跳转报错:There are multiple heroes that share the same tag within a subtree.

以上是关于Flutter踩坑记: There are multiple heroes that share the same tag within a subtree.的主要内容,如果未能解决你的问题,请参考以下文章

Spring @Transactional踩坑记

HttpWebRequest 改为 HttpClient 踩坑记-请求头设置

FastDFS踩坑记

iptables踩坑记

windows container 踩坑记

踩坑记