In Flutter - How to pass the image downloaded from the url

2.3k Views Asked by At

Through the API I retrieve data of an object - including its url. I show on one screen an image with url (Image.Network()). Now, after clicking on this image on the list, I want to move to a single image view - how do I pass the image, not the url, to a new view so that the application doesn't have to download it from the url again?

2

There are 2 best solutions below

1
On BEST ANSWER

you can pass the image to Other Class like this

Navigator.push(
          context, MaterialPageRoute(builder: (context) => OtherClass(data: image)));

and in Other class receive the image like

   var data;

   OtherClass({Key key, @required this.data}) : super(key: key);
0
On

My code:

onTap: () {
            Navigator.push(context, MaterialPageRoute(builder: (_) {
            return DetailScreen(url: users[index].url);
        }));
    },
[...]
class DetailScreen extends StatelessWidget {
    final String url;

    DetailScreen({Key key, @required this.url})
        : assert(url != null),
            super(key: key);
    @override
    Widget build(BuildContext context) {
        return Scaffold(
            body: GestureDetector(
                child: Center(
                    child: PhotoView(
                        imageProvider: NetworkImage(url),
                    )
                ),
                onTap: () {
                    Navigator.pop(context);
                },
            ),
        );
    }
}