如果用户成功创建了一个项目,我将使用新的ScaffoldMessenger来显示小吃栏。 显示小吃栏时,我将应用程序导航到仪表板。但是,一旦击中仪表板,就会在子树中共享同一标签的多个英雄会引发错误。 我在仪表板上没有使用任何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
...
答案 0 :(得分:6)
我遇到了同样的问题。如果您有嵌套的脚手架,就会发生这种情况。 ScaffoldMessenger 想要将 Snackbar 发送到所有 Scaffolds。要解决此问题,您需要使用 ScaffoldMessenger 包裹您的 Scaffold。这可确保您只有一个 Scaffold 收到 Snackbar。
ScaffoldMessenger(
child: Scaffold(
body: ..
),
)
答案 1 :(得分:3)
我通过在下一个事件循环迭代中运行调用解决了这个问题:
Future.delayed(const Duration(), () =>
ScaffoldMessenger.of(context).showSnackBar(SnackBar(...)));
答案 2 :(得分:1)
我遇到了同样的问题,并通过在用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
希望对你有用
答案 3 :(得分:1)
遇到了同样的问题,结果我有一个脚手架小部件在我的子树中返回另一个脚手架(哎呀)
如果是这样,那么您的小吃店会在两个脚手架上弹出,然后启动转换会导致错误。