在开发中使用到了Hero组件,然后碰到如下报错:
1 2 3 4 5 6 7 8 |
I/flutter: ══╡ EXCEPTION CAUGHT BY SCHEDULER LIBRARY ╞═════════════════════════════════════════════════════════ I/flutter: The following assertion was thrown during a scheduler callback: I/flutter: There are multiple heroes that share the same tag within a subtree. I/flutter: Within each subtree for which heroes are to be animated (i.e. a PageRoute subtree), each Hero must I/flutter: have a unique non-null tag. I/flutter: In this case, multiple heroes had the following tag: <default FloatingActionButton tag> I/flutter: Here is the subtree for one of the offending heroes: I/flutter: # Hero(tag: <default FloatingActionButton tag>, state: _HeroState#c0b28) |
当时在业务中使用,没想到此错误是Hero组件引起的,所以排查起来找了有些时间,对此Hero组件又有了新的认识。
在程序回调的的时候发生了以下断言:
在子树中有多个Hero 对象共用了一个tag,每个Hero对象的tag必须是唯一的。
1 2 3 4 5 6 7 8 9 10 11 |
// 组件1 Hero( tag: 'jonhuu.com', child: Container(), ) // 组件2 Hero( tag: 'jonhuu2', // 切勿重复一样 child: Container(), ) |
在同一个页面中使用多个Hero组件时,一定要注意tag不能重复名称。因为当时我的tag是动态生成的,所以会出现重复的情况
在Hero组件是以tag来作为唯一标识,来确认二个Hero组件相同tag来做切换动画效果,如果有三个tag及以上名称都是一样的,这时候该组件是无法确定一一对应关系,所以引起报错。
另外还有一种场景下面会碰到重复tag的坑,那么就是在使用tabs组件切换时(比如首页底部tabs切面不同区域),不同的内容区域页面组件,如果都使用了Hero组件,并且tag名称也相同,这也是会报错的。
注意:以上这个情况你是非常难找到的,因为每个页面你都单独处理,误以为都是独立页面。