best-flutter / flutter_swiper

The best swiper for flutter , with multiple layouts, infinite loop. Compatible with Android & iOS.
MIT License
3.51k stars 713 forks source link

There are multiple heroes that share the same tag within a subtree. #239

Open fernando-s97 opened 4 years ago

fernando-s97 commented 4 years ago

Hi!

My Swiper list (EventsList) has 2 items, but 3 items appear in the widget tree. image And as I'm using the Hero widget, I'm getting the following error image How can I solve that?

In this case, the 1st and 3rd items are the same.

realshovanshah commented 3 years ago

I was having the same problem. This seems to be the occurring because the Swiper widget build the first item twice, at the top and the bottom of the stack. This can be seen here (the first item, 1, is built two times ):

image

This means that any widget at the top of the stack will have a duplicate widget thus all the property passed to that widget are also duplicated. (No matter how unique the data id)

What worked for me:

Defining a global id (say 'heroId') and incrementing it on every build:

image

We use this, 'heroId' as the hero tag by passing it on new when navigating to the target screen via the constructor:

image

Hope this helped you!