“ StatefulElement”类型不是“ Context”类型的子类型-小部件库捕获到异常

时间:2020-09-17 23:41:13

标签: flutter dart

Flutter应用程序产生以下错误:

类型“ StatefulElement”不是“上下文”类型的子类型

我正在使用以下软件包:PDFPrinting

我被告知错误的可能原因是调用_visualizarPdf (context, path)函数

_visualizarPdf 函数使用flutter_full_pdf_viewer包显示PDF,并且可以正常工作。单击打印图标时会发生错误。

根据软件包printing,负责生成印象的函数是打印机软件包的Printing.layoutPdf。

在我试图在_visualizarPdf函数外部的StatelessWidget中传递_visualizarPdf之前,但是发生了另一个错误,当以这种方式传递时,属性返回null,而不生成PDF

import 'dart:io';
import 'package:flutter_full_pdf_viewer/full_pdf_viewer_scaffold.dart';
import 'package:pdf/widgets.dart' as pw;
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:pdf/pdf.dart';
import 'package:printing/printing.dart';

Future<Uint8List> segundaViaReport(context, Orgao orgao) async {
    final pw.Document pdf = pw.Document();
    final String nomeMunicipio = orgao.descricao;

    pw.Widget _header(pw.Context context) {
        return pw.Column(
            children: <pw.Widget>[
                pw.Text(nomeMunicipio, textScaleFactor: 2),            
                pw.Divider(height: 2.0),
            ],
        );
    }

    Widget _visualizarPdf(pw.Context context, pathDirectory) {
        final String path = pathDirectory;
    
        return PDFViewerScaffold(
        appBar: AppBar(
            title: Text(""),
            actions: <Widget>[
            IconButton(icon: Icon(Icons.print), onPressed: () {
                Printing.layoutPdf(onLayout: (format) => segundaViaReport(context, orgao));
            }),
            ],
        ),
        path: path,
        );
    }

    pdf.addPage(
        pw.MultiPage(
        orientation: pw.PageOrientation.portrait,
        margin: pw.EdgeInsets.all(3.0 * PdfPageFormat.mm),
        header: _header,
        build: (pw.Context context) => [
            pw.Wrap(children: <pw.Widget>[
            pw.Column(
                crossAxisAlignment: pw.CrossAxisAlignment.start,
                children: <pw.Widget>[              
                _IdentificarObservacoes(orgao),
                ],
            ),
            ]),
        ],
        ),
    );

    final String dir = (await getApplicationDocumentsDirectory()).path;
    final String path = '$dir/segunda_via.pdf';
    final File file = File(path);
    await file.writeAsBytes(pdf.save());
    Navigator.of(context).push(
        MaterialPageRoute(
        builder: (_) => _visualizarPdf(context, path),
        ),
    );
}

0 个答案:

没有答案