3

I am using flutter printing library to make selected images as pdf. I have a list and inside that list there are paths of the selected images. Here is the code:

final doc = pw.Document();


    for(var i=0;i>selected.length;i++){
      Image img=Image.file(File(selected[i]));
      ImageProvider image=img.image;



      doc.addPage(pw.Page(
          build: (pw.Context context) {
            return pw.Center(
              child: pw.Image(image),
            ); // Center
          })); // Page




    }

pw.Image only accepts ImageProvider but i am getting this error:

The argument type 'ImageProvider<Object>' can't be assigned to the parameter type 'ImageProvider'.

How can i solve this problem?

cvsrt
  • 357
  • 4
  • 19

2 Answers2

5

Call ImageProvider from "pw" like this...

doc.addPage(pw.Page(
          pageFormat: PdfPageFormat.a4,
          build: (pw.Context context) {
            return pw.Center(
              child: pw.Image(pw.MemoryImage(imageBytes)),
            ); // Center
          })); // Page
1

Its currently an issue with flutter . To get around it ,you can cast the object as an Image Provider .

child: pw.Image(image) as ImageProvider

This has also been discussed here .

austin
  • 517
  • 5
  • 16
  • 1
    Now i am getting this error: The argument type 'ImageProvider' can't be assigned to the parameter type 'Widget?'. – cvsrt Sep 28 '21 at 17:06
  • The linked question is about passing an `Object` as an `ImageProvider`. That is a different error, and that requires a cast because implicit downcasts are intentionally no longer allowed by default since they're unsafe. – jamesdlin Sep 28 '21 at 20:21