颤振:发生异常。 “NoSuchMethodError:在 null 上调用了 getter 'length'。接收者:空尝试调用:长度”

时间:2021-02-21 10:51:34

标签: flutter flutter-dependencies

我使用 screenShot package 拍摄页面图像,然后将其转换为 pdf 以使用 Printing package 打印此页面。

这是导致错误的行:

 await Printing.layoutPdf( onLayout: (PdfPageFormat format) async => pdf.save());

这是我收到的唯一消息

<块引用>

发生异常。 “NoSuchMethodError:在 null 上调用了 getter 'length'。接收者:空尝试调用:长度”

编辑器中还会出现一个新页面,其中突出显示了以下几行:

`await_channel.invokeMethod<int>('printPdf', params); try { return await job.onCompleted.future;} finally { _printJobs.remove(job.index);     }}`

应用程序也关闭并显示此消息不幸的是,后台打印程序已停止

整个功能:

//Convert image to pdf and print it
  printPdf(Uint8List screenShot) async {
    var pdf = pw.Document();

// Create pdf
    pdf.addPage(
      pw.Page(
        margin: pw.EdgeInsets.all(0),
        build: (context) {
          return pw.Expanded(
            child: pw.Image(
              pw.MemoryImage(screenShot),
              fit: pw.BoxFit.fill,
            ),
          );
        },
      ),
    );

// Printing
    await Printing.layoutPdf(
        onLayout: (PdfPageFormat format) async => pdf.save());
  }
}

全班:

class _ShowNotesState extends State<ShowNotes> {
  final ScreenshotController screenshotController = ScreenshotController();

  Uint8List _image;

  TextStyle _hesderStyle = TextStyle(
    fontSize: 20,
  );

  @override
  Widget build(BuildContext context) {
    return Directionality(
      textDirection: TextDirection.rtl,
      child: Scaffold(
        appBar: AppBar(
          title: Text("الرئيسية"),
          actions: [
            InkWell(
              onTap: () {
                Navigator.of(context).push(
                  MaterialPageRoute(
                    builder: (_) => AddNotes(),
                  ),
                );
              },
              child: Padding(
                padding: const EdgeInsets.only(left: 10),
                child: Row(
                  children: [
                    Text(
                      "إضافة روشتة",
                      style: TextStyle(color: Colors.white, fontSize: 20),
                    ),
                    const SizedBox(width: 5),
                    Icon(FontAwesomeIcons.filePrescription),
                  ],
                ),
              ),
            ),
          ],
        ),
        body: Screenshot(
          controller: screenshotController,
          child: Padding(
            padding: const EdgeInsets.symmetric(vertical: 20, horizontal: 10),
            child: widget.doctorName == null
                ? Text("")
                : Column(
                    crossAxisAlignment: CrossAxisAlignment.stretch,
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text("مستشفى الأمل",
                              style: TextStyle(
                                fontSize: 20,
                                fontWeight: FontWeight.bold,
                              )),
                          Text("Hope Hospital",
                              style: TextStyle(
                                fontSize: 25,
                                fontWeight: FontWeight.bold,
                              )),
                        ],
                      ),
                      const SizedBox(height: 15),
                      Text("الطبيب:  ${widget.doctorName}",
                          style: _hesderStyle),
                      const SizedBox(height: 5),
                      Text("القسم: ${widget.department}", style: _hesderStyle),
                      const SizedBox(height: 5),
                      Text("اسم المريض: ${widget.patientName}",
                          style: _hesderStyle),
                      const SizedBox(height: 5),
                      Row(
                        children: [
                          Text("السن: ${widget.age}  سنة",
                              style: TextStyle(
                                fontFamily: GoogleFonts.cairo().fontFamily,
                                fontSize: 20,
                              )),
                          Spacer(),
                          Text("الوظيفة: ${widget.job}", style: _hesderStyle),
                          Spacer(),
                        ],
                      ),
                      const SizedBox(height: 15),
                      Text(
                        "الأدوية:",
                        style: _hesderStyle,
                      ),
                      const SizedBox(height: 10),
                      Directionality(
                        textDirection: TextDirection.ltr,
                        child: Text(
                          "${widget.drugName}",
                          style: TextStyle(fontSize: 18),
                        ),
                      )
                    ],
                  ),
          ),
        ),
        floatingActionButton: FloatingActionButton(
          child: Icon(FontAwesomeIcons.print),
          onPressed: () {
            //Take screenshoot
            screenshotController.capture().then((Uint8List image) {
              setState(() {
                _image = image;
              });
            }).catchError((onError) {
              print(onError);
            });
            printPdf(_image);
          },
        ),
      ),
    );
  }

0 个答案:

没有答案