Home > Back-end >  In Flutter, what should I supply to the tag of Hero?
In Flutter, what should I supply to the tag of Hero?

Time:05-24

I often get errors about "There are multiple heroes that share the same tag within a subtree."

For now, I have supplied the tag with Uuid().v4(). I'm pretty sure that this will be completely random. But I still got these errors.

Here is the subtree for one of the offending heroes: Hero
  tag: f63e9e14-55e4-4655-ab96-18b8dbbff524
  state: _HeroState#dc119

How can I generate something that will be completely random every time it's called? Or did I use this Hero widget incorrectly?

FullScreenWidget(child: Hero(child: image(fit: fit), tag: Uuid().v4()));

CodePudding user response:

Just provide one unique id for both hero widgets. F.e.

On first screen

Screen1(child: Hero(child: image(fit: fit), tag: "Hero1tag"));

On second screen:

Screen2(child: Hero(child: image(fit: fit), tag: "Hero1tag"));

Tag must be unique and stable for all time your app work.

CodePudding user response:

It doesn't need to be unique every time it's called, it needs to be unique per route. In total there should be two of them, one as source hero tag and other as destination hero tag. You can use something descriptive like 'user-profile-picture'. Keep in mind that the tag could be any other object, like a UserModel or something like that.

  • Related