How do I feed an ImageProvider with an image object(Flutter)

Issue

I have this PhotoView from photo_view: ^0.10.3. I want to use the Image object to the PhotoView but it need an ImageProvider, I don’t know how to do it. I know AssetsImage() can be feed but my image is not from the asset and I already made an Image object with the Image I want.

import 'package:photo_view/photo_view.dart';
import 'package:flutter/material.dart';



class PlaceSelectedImage extends StatefulWidget{
  final Image _image;
  PlaceSelectedImage(this._image);
  @override
  PlaceSelectedImageState createState() => PlaceSelectedImageState(_image);
}

class PlaceSelectedImageState extends State<PlaceSelectedImage>{
  final Image _image;
  PlaceSelectedImageState(this._image);
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Center(
      child: PhotoView(
          imageProvider: _image
      ),
    );
  }

}

Solution

If you want to use an image from the server you need to use NetworkImage class instead of using AssetImage.

You can’t use Image, because it’s a different widget by itself and it consumes Asset/NetworkImage.

Answered By – erknvl

Answer Checked By – Willingham (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *