Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ScaffoldMessenger throws a hero animation error

Tags:

flutter

dart

I am using the new ScaffoldMessenger to show a snackbar if a user successfully creates a project. While showing the snackbar, i navigate the app to the dashboard. But as soon as it hits the dashboard There are multiple heroes that share the same tag within a subtree error is thrown. I am not using any Hero widget in my dashbard and I have one FloatingActionButton but its hero parameter is set to null.

Sample code:

ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(
          content: Text('A SnackBar has been shown.'),
          animation: null,
        ),
      );
Navigator.pushReplacementNamed(context, '/dashboard');

Which results in this error:

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
...


like image 605
The Star Avatar asked Nov 04 '20 09:11

The Star


Video Answer


2 Answers

I had the same problem. This happens if you have nested Scaffolds. The ScaffoldMessenger wants to send the Snackbar to all Scaffolds. To fix this you need to wrap your Scaffold with a ScaffoldMessenger. This ensures you that only one of your Scaffold receives the Snackbar.

ScaffoldMessenger(
  child: Scaffold(
    body: ..
    ),
)
like image 168
quoci Avatar answered Oct 13 '22 18:10

quoci


I ran into same problem and fixed it by removing SnackBar before any call to Navigator with ScaffoldMessenger.of(context).removeCurrentSnackBar().

Look like this with your Sample code:

ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(
          content: Text('A SnackBar has been shown.'),
          animation: null,
        ),
      );
ScaffoldMessenger.of(context).removeCurrentSnackBar();
Navigator.pushReplacementNamed(context, '/dashboard');

Here's the link that helped me : https://flutter.dev/docs/release/breaking-changes/scaffold-messenger#migration-guide
Hope it'll work for you

like image 6
GreenFrog Avatar answered Oct 13 '22 18:10

GreenFrog