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
The
Image
constructor frompdf
package is expecting a parameter of typeImageProvider
that also comes from the same package. You are providing anImageProvider
from the Flutter framework, which is why the error shown.Use one of the classes that extends
ImageProvider
from pdf package to create anImageProvider
that can be passed to thepw.Image
constructor.MemoryImage
andRawImage
allows you to create anImageProvider
from aUint8List
.Read this documentation for more example: https://pub.dev/packages/pdf#examples
in your case, you need to use
MemoryImage
now you can use it as