Getting this error while trying to save pdf file which I am creating :
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:path_provider/path_provider.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
class PrintBarcodeScreen extends StatefulWidget {
@override
_PrintBarcodeScreenState createState() => _PrintBarcodeScreenState();
}
class _PrintBarcodeScreenState extends State<PrintBarcodeScreen> {
var dt = DateTime.now();
final pdf = pw.Document();
generatePdf() {
pdf.addPage(
pw.MultiPage(
pageFormat: PdfPageFormat.a4,
margin: pw.EdgeInsets.all(16),
build: (pw.Context context) {
return <pw.Widget>[
pw.Header(
level: 0,
child: pw.Row(
mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
children: [
pw.Text(
DateFormat.yMMMEd().format(dt),
textScaleFactor: 2,
// style: getTheme.textTheme.headline6,
),
pw.Text(
DateFormat.jms().format(dt),
// style: getTheme.textTheme.headline6,
),
],
),
),
pw.BarcodeWidget(
barcode: pw.Barcode.gs128(),
color: PdfColor.fromHex("#000000"),
data: '123456789123',
// width: screenSize.width * 0.8,
height: 90,
),
pw.Table.fromTextArray(context: context, data: <List<String>>[
<String>['Year', 'Ipsum', 'Lorem'],
<String>['SN0', 'GFG1'],
<String>['SN1', 'GFG2'],
]),
];
}),
);
}
Future savePdf() async {
Directory documentDirectory = await getApplicationDocumentsDirectory();
String documentPath = documentDirectory.path;
File receiptFile = File("$documentPath/receipt.pdf");
receiptFile.writeAsBytesSync(pdf.save()); // this line is showing error pdf.save() is underlined red
}
@override
Widget build(BuildContext context) {
.........
}
Screenshot:
Couldn't find much about it anywhere. Not much experienced yet with flutter to understand the depth of this error. Little I do understand that this is data type mismatch situation. I saw writeAsBytesSync has List bytes parameter, could figure out how can I change my pdf data to int.
Add await
before pdf.save()
receiptFile.writeAsBytesSync(await pdf.save());
As pointed out by @Nishuthan, we should use the await
keyword to resolve the Future
and convert the Uint8List
to List<int>
by the following means:
Future savePdf() async {
Directory documentDirectory = await getApplicationDocumentsDirectory();
String documentPath = documentDirectory.path;
File receiptFile = File("$documentPath/receipt.pdf");
receiptFile.writeAsBytesSync(List.from(await pdf.save()));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With