2

I am creating a PDF document with dynamic data. However, I am not able to show the PDF and I get the error

"This widget created more than 20 pages. This may be an issue in the widget or the document."

i used listview builder but not working check below code and help me

import 'package:pdf/pdf.dart'; import 'package:pdf/widgets.dart' as pw;

pdf.addPage(

      pw.MultiPage(

      header: (pw.Context context) {
        if (context.pageNumber == 1) {
          return null;
        }
        return pw.Container(
            alignment: pw.Alignment.centerRight,
            margin:
                const pw.EdgeInsets.only(bottom: 3.0 * PdfPageFormat.mm),
            padding:
                const pw.EdgeInsets.only(bottom: 3.0 * PdfPageFormat.mm),
            child: pw.Text(reportListDetail[i].location,
                style: pw.Theme.of(context)
                    .defaultTextStyle
                    .copyWith(color: PdfColors.grey)));
      },
      footer: (pw.Context context) {
        return pw.Container(
            alignment: pw.Alignment.centerRight,
            margin: const pw.EdgeInsets.only(top: 1.0 * PdfPageFormat.cm),
            child: pw.Text(
                'Page ${context.pageNumber} of ${context.pagesCount}',
                style: pw.Theme.of(context)
                    .defaultTextStyle
                    .copyWith(color: PdfColors.grey)));
      },
      build: (pw.Context context) => <pw.Widget>[

          pw.Wrap
            (
              children: <pw.Widget>[

                pw.ListView(
                  children: [
                   /* pw.Container(
                      margin: pw.EdgeInsets.only(top: 20, bottom: 30),
                      alignment: pw.Alignment.center,
                      child: pw.Text(reportListDetail[i].location),
                    ),*/
                    pw.ListView.builder(
                        itemCount: reportListDetail.length,
                        itemBuilder: (pw.Context ctxt, int main) {
                          return pw.ListView(
                              children:
                              [
                            pw.Container(
                              margin: pw.EdgeInsets.only(top: 20, bottom: 10),
                              alignment: pw.Alignment.topLeft,
                              child:
                              pw.Text(reportListDetail[main].subjectName),
                            ),

                           pw.GridView(
                                  crossAxisCount: 2,
                                  childAspectRatio: 1,
                                  children: List.generate(
                                      reportListDetail[main].subjctList.length,
                                          (index) {
                                        bool isMatched = false;
                                        var image;
                                        var d = reportListDetail[main]
                                            .subjctList[index]
                                            .imageName
                                            .split(".");
                                        String type = "";

                                        for (int i = 0; i < d.length; i++) {
                                          if (i == (d.length - 1)) {
                                            type = d[i];
                                          }
                                        }

                                        String typeLowerCase = type.toLowerCase();

                                        if (videoExtensionList
                                            .contains(typeLowerCase)) {
                                          setState(() {
                                            isMatched = true;
                                          });
                                        } else {
                                          setState(() {
                                            isMatched = false;
                                          });
                                        }
                                        return pw.Container(
                                            child: pw.ListView(children: [
                                              pw.Container(
                                                  alignment: pw.Alignment.topLeft,
                                                  margin: pw.EdgeInsets.only(left: 10),
                                                  child: pw.Text(reportListDetail[main]
                                                      .subjctList[index]
                                                      .captionName)),
                                              isMatched
                                                  ? pw.Flexible(
                                                  child: pw.SizedBox(
                                                      width: 350,
                                                      height: 200,
                                                      child: pw.FittedBox(
                                                          fit: pw.BoxFit.fitWidth,
                                                          alignment:
                                                          pw.Alignment.topLeft,
                                                          child: pw.Text(
                                                            "https://drive.google.com/uc?export=download&id=" +
                                                                reportListDetail[main]
                                                                    .subjctList[index]
                                                                    .id,
                                                            maxLines: 3,
                                                          ))))
                                                  : pw.Image.provider(
                                                  pw.MemoryImage(File(
                                                      reportListDetail[main]
                                                          .subjctList[index]
                                                          .imageName)
                                                      .readAsBytesSync()),
                                                  width: 200,
                                                  height: 200,
                                                  fit: pw.BoxFit.cover),
                                            ]));
                                      }),
                                ),


                          ]);
                        }),
                  ],
                )
              ]
          )


          ]
      )
  );
dixita
  • 94
  • 5
  • Try looking here: https://stackoverflow.com/questions/61679628/flutter-pdf-error-this-widget-created-more-than-20-pages-this-may-be-an-i – il_boga Feb 05 '21 at 11:00

1 Answers1

0

Select MultiPage by clicking ctrl+ MultiPage There you'll see a page object change it to how much you want....

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 09 '22 at 01:03