如果用户成功创建项目,我将使用新的 ScaffoldMessenger 来显示小吃栏。在显示小吃店时,我将应用导航到仪表板。但是一旦它点击仪表板,就会抛出一个子树错误中共享相同标签的多个英雄。我没有在仪表板中使用任何 Hero 小部件,我有一个 FloatingActionButton 但它的 hero 参数设置为 null。
示例代码:
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('A SnackBar has been shown.'),
animation: null,
),
);
Navigator.pushReplacementNamed(context, '/dashboard');
Run Code Online (Sandbox Code Playgroud)
这导致此错误:
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
...
Run Code Online (Sandbox Code Playgroud)
我有同样的问题。如果您有嵌套的脚手架,就会发生这种情况。ScaffoldMessenger 想要将 Snackbar 发送到所有 Scaffolds。要解决此问题,您需要使用 ScaffoldMessenger 包裹您的 Scaffold。这确保您只有一个 Scaffold 收到 Snackbar。
ScaffoldMessenger(
child: Scaffold(
body: ..
),
)
Run Code Online (Sandbox Code Playgroud)
小智 5
我遇到了同样的问题,并通过在调用Navigatorwith之前删除 SnackBar 来修复它ScaffoldMessenger.of(context).removeCurrentSnackBar()。
使用您的示例代码看起来像这样:
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('A SnackBar has been shown.'),
animation: null,
),
);
ScaffoldMessenger.of(context).removeCurrentSnackBar();
Navigator.pushReplacementNamed(context, '/dashboard');
Run Code Online (Sandbox Code Playgroud)
这是帮助我的链接:https : //flutter.dev/docs/release/break-changes/scaffold-messenger#migration-guide
希望它对你有用
| 归档时间: |
|
| 查看次数: |
1582 次 |
| 最近记录: |