Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The argument type 'Future<Uint8List>' can't be assigned to the parameter type 'List<int>' [duplicate]

Tags:

flutter

dart

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: 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.

like image 912
Priyank Sharma Avatar asked Dec 30 '22 15:12

Priyank Sharma


2 Answers

Add await before pdf.save()

 receiptFile.writeAsBytesSync(await pdf.save());
like image 149
Nishuthan S Avatar answered May 24 '23 18:05

Nishuthan S


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()));
  }
like image 21
Stefano Amorelli Avatar answered May 24 '23 17:05

Stefano Amorelli