创建pdf in flutter错误:Assert失败:第117行位置12:'!_paint':文档已保存

7dl7o3gd  于 2023-05-08  发布在  Flutter
关注(0)|答案(2)|浏览(122)

我是新的Flutter和面临一些问题时,以下PDF发票教程。一切工作正常的第一次打印PDF,但对于第二次尝试后,添加/编辑一些数据,PDF文件仍然是第一个PDF,我得到了错误,根据标题。
未处理异常:'package:pdf/src/widgets/document. dart':Assert失败:第117行位置12:“!_paint”:文档已保存。
下面是我的部分代码

onClicked: () async {
                  
                  final date = DateTime.now();
                  final dueDate = date.add(Duration(days: 7));

                  final invoice = Invoice(
                    supplier: Supplier(
                      name: '..',
                      address: '000',
                      paymentInfo: '**.com',
                    ),
                    info: InvoiceInfo(
                      date: date,
                      dueDate: dueDate,
                      description: data.texttobeinsert,
                      number: '${DateTime.now().year}-9999',
                    ),
                    infoYourSignature: InvoiceInfo2(
                      date: date,
                      dueDate: dueDate,
                      description: data.texttobeinsert2,
                      number: '${DateTime.now().year}-9999',
                    ),
                    items: [
                      InvoiceItem(
                        description:
                    
                            AppLocalizations.of(context)!
                                .translate('net_monthly_incomedsr')!,
                        unitPrice: AppLocalizations.of(context)!
                                .translate('currency')! +
                            oCcy1.format(data.netmonthlyincome),
                      ),
                      InvoiceItem(
                        description: AppLocalizations.of(context)!
                            .translate('other_income')!,
                        //  date: DateTime.now(),
                        //    quantity: 8,
                        //     vat: 0.19,
                        unitPrice: AppLocalizations.of(context)!
                                .translate('currency')! +
                            oCcy1.format(data.otherincome),
                      ),
                      InvoiceItem(
                          description: data.friendlist1.isEmpty
                              ? "[n/a]"
                              : "[" +
                                  data.friendlist1 +
                                  ", " +
                                  data.friendlist2 +
                                  "]",
                  
                          unitPrice: ""),
                          InvoiceItem(
                        description:
                            AppLocalizations.of(context)!.translate('ndi')!,

                
                        unitPrice: AppLocalizations.of(context)!
                                .translate('currency')! +
                            oCcy1.format(data.ndi),
                      ),
                    ],
                  );

                  final pdfFile = await PdfInvoiceApiDsr.generate(invoice);
                  PdfApi.openFile(pdfFile);
                },
              ),

PdfInvoiceApiDsr.dart(部分)

import '../api/pdf_api.dart';
import '../model/customer.dart';
import '../model/invoice.dart';
import '../model/supplier.dart';
import '../utils.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
import 'package:pdf/widgets.dart';

int runonceforprinting = 1;
final pdf = pw.Document();

final imageA = PdfImage.file(
  pdf.document,
  bytes: File('assets/icon/icon2.png').readAsBytesSync(),
);
var profileImage;

class PdfInvoiceApiDsr {
  static Future<File> generate(Invoice invoice) async {
    profileImage = pw.MemoryImage(
      (await rootBundle.load('assets/icon/icon2.png')).buffer.asUint8List(),
    );
   

    pdf.addPage( <----this error from here, it tells the error :  the _paint has been saved!..
      pw.MultiPage(
        build: (context) => [
          buildHeader(invoice),
          SizedBox(height: 0.05 * PdfPageFormat.cm),
          buildTitle(invoice),
       
          buildInvoice(invoice),
        
        ],
        footer: (context) => buildFooter(invoice),
      ),
    );

....
PDfApi

class PdfApi {
  static Future<File> saveDocument({
    required String name,
    required Document pdf,
  }) async {
    final bytes = await pdf.save();

    final dir = await getApplicationDocumentsDirectory();
    final file = File('${dir.path}/$name');

    file.writeAsBytesSync(bytes);

    return file;
  }

  static Future openFile(File file) async {
    final url = file.path;

    await OpenFile.open(url);
  }
}
nfg76nw0

nfg76nw01#

我也有同样的问题,但我解决它像这样尝试添加final pdf = pw.Document()在你的方法generate(Invoice invoid),所以当你离开页面,你的pdf文件也将被删除,你总是创建一个新的pdf文件。
所以:

class PdfInvoiceApiDsr {
  static Future<File> generate(Invoice invoice) async {
   profileImage = pw.MemoryImage(
    (await rootBundle.load('assets/icon/icon2.png')).buffer.asUint8List(),
   );
   final pdf = pw.Document()

pdf.addPage( <----this error from here, it tells the error :  the _paint has been saved!..
  pw.MultiPage(
    build: (context) => [
      buildHeader(invoice),
      SizedBox(height: 0.05 * PdfPageFormat.cm),
      buildTitle(invoice),
   
      buildInvoice(invoice),
    
    ],
    footer: (context) => buildFooter(invoice),
  ),
);`
7d7tgy0s

7d7tgy0s2#

我也遇到了同样的错误,为了解决这个问题,每当我们执行一个动作时,我们必须调用下面的代码:

final pdf = pw.Document();

相关问题