skip to Main Content

I’m trying to display an Image in a PDF, using the Flutter pdf package.

pw.Image( myImage ),

This line is giving me the error

The argument type ‘ImageProvider<Object>’ can’t be assigned to the parameter type ‘ImageProvider’

I tried adding a typecast

pw.Image( myImage as ImageProvider),

But then I just get an "unnecessary cast error", in addition to the other error.

This is the line where I’m defining myImage :

var myImage = FileImage( File('path-to-the-image/image.png') );

Is there a difference between ImageProvider<Object> and ImageProvider ?

How do I make the type cast / tranlation ?

2

Answers


  1. The Image constructor from pdf package is expecting a parameter of type ImageProvider that also comes from the same package. You are providing an ImageProvider from the Flutter framework, which is why the error shown.

    Use one of the classes that extends ImageProvider from pdf package to create an ImageProvider that can be passed to the pw.Image constructor. MemoryImage and RawImage allows you to create an ImageProvider from a Uint8List.

    Login or Signup to reply.
  2. Read this documentation for more example: https://pub.dev/packages/pdf#examples

    in your case, you need to use MemoryImage

    final  myImage = pw.MemoryImage(File('path-to-the-image/image.png'));
    

    now you can use it as

    pw.Center( child: pw.Image(image));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search