如果用户成功创建了一个项目,我将使用新的ScaffoldMessenger来显示一个 snackbar 。
显示 snackbar 时,我将应用程序导航到仪表板。但是,一旦击中仪表板,子树中就会有多个共享相同标签的英雄会引发错误。
我在仪表板上没有使用任何Hero小部件,并且有一个FloatingActionButton,但其hero参数设置为null。
示例代码:
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('A SnackBar has been shown.'),
animation: null,
),
);
Navigator.pushReplacementNamed(context, '/dashboard');
导致此错误: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 (i.e. a PageRoute subtree), each Hero must have a unique non-null tag.
In this case, multiple heroes had the following tag: <SnackBar Hero tag - Text("A SnackBar has been shown.")>
Within each subtree for which heroes are to be animated (i.e. a PageRoute subtree), each Hero must have a unique non-null tag.
In this case, multiple heroes had the following tag: <SnackBar Hero tag - Text("A SnackBar has been shown.")>
Here is the subtree for one of the offending heroes: Hero
tag: <SnackBar Hero tag - Text("A SnackBar has been shown.")>
state: _HeroState#7589f
When the exception was thrown, this was the stack
#0 Hero._allHeroesFor.inviteHero.<anonymous closure>
#1 Hero._allHeroesFor.inviteHero
package:flutter/…/widgets/heroes.dart:277
#2 Hero._allHeroesFor.visitor
package:flutter/…/widgets/heroes.dart:296
#3 ComponentElement.visitChildren
package:flutter/…/widgets/framework.dart:4729
#4 Hero._allHeroesFor.visitor
package:flutter/…/widgets/heroes.dart:309
...
最佳答案
我遇到了同样的问题,并通过使用Navigator
调用ScaffoldMessenger.of(context).removeCurrentSnackBar()
之前删除了SnackBar来解决了该问题。
带有您的的样例代码:
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('A SnackBar has been shown.'),
animation: null,
),
);
ScaffoldMessenger.of(context).removeCurrentSnackBar();
Navigator.pushReplacementNamed(context, '/dashboard');
这是对我有帮助的链接:https://flutter.dev/docs/release/breaking-changes/scaffold-messenger#migration-guide希望对你有用
关于flutter - ScaffoldMessenger抛出英雄动画错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64677196/