5

I am scratching my head to find an easy way to make a hero animation work when the target hero is in a FutureBuilder. I know this is not working because the hero image has to be be present on the very first frame of the second screen, which obviously cannot be the case when getting the data from Firestore through a FutureBuilder.

Here is my code:

First screen:

 Hero(
    tag: "postImage",
    child: Image.network(post.imageurl),
  ),

Second Screen:

FutureBuilder(
      future: _futurePost,
      builder: (context, AsyncSnapshot snapshot) {
      [...]
      return  Hero(
        tag: "postImage",
        child: Image.network(snapshot.data.imageurl),
      );
    }
  )

How to achieve this? I am pretty surprised this is not a common question!

Thank you!

Mat
  • 81
  • 4
  • Is the `post.imageurl` going to be the same image as `snapshot.data.imageurl`? – Will Hlas Mar 11 '22 at 17:18
  • yes, but I am getting the post infos from firestore on each screen – Mat Mar 12 '22 at 19:21
  • 1
    If it's the same image on both screens, and you get that image on the first screen, then there is no need to use `FutureBuilder` on the second screen, right? – Aleksandar Mar 18 '22 at 18:31
  • 1
    Sorry for thedelay just getting back at the project. Yes your're right, but sometimes I am landing on the secondpage directly from a notification, so yes in this case no Hero animation.The thing is that I want only one piece of code for both cases, do you get me? – Mat Jul 21 '22 at 15:52

0 Answers0