flutter - ScaffoldMessenger抛出英雄动画错误

标签 flutter dart

如果用户成功创建了一个项目,我将使用新的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/

相关文章:

flutter - 对两个文本小部件仅使用一次“对齐”

flutter - 如何在 flutter 中将彩色线设置到卡片末尾?

flutter - Flutter 中调用 Future 和 Future.microtask 有什么区别?

flutter - Cupertino widgets 会在 android 上运行吗

flutter - Flutter-如何检查从哪个屏幕单击了底部导航选项卡

flutter - 当高度较短时,列内的子项收缩,当高度溢出时可滚动

flutter - 如何在 ListTile 中添加阴影,如 'elevation'

firebase - Firestore映射到StreamBuilder => ListView.Builder

Android 模拟器不处理 http get 请求,但 iOS 模拟器工作正常

android - 无法访问类 'com.google.common.util.concurrent.ListenableFuture' 。检查模块类路径是否缺少或冲突的依赖项