【问题标题】:The argument type 'Future<Uint8List>' can't be assigned to the parameter type 'List<int>' [duplicate]参数类型“Future<Uint8List>”不能分配给参数类型“List<int>”[重复]
【发布时间】:2021-07-20 14:12:17
【问题描述】:

尝试保存我正在创建的 pdf 文件时出现此错误:

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) {
    .........
}

截图:

在任何地方都找不到太多关于它的信息。对颤振没有太多经验来理解这个错误的深度。我不明白这是数据类型不匹配的情况。我看到 writeAsBytesSync 有 List bytes 参数,可以弄清楚如何将我的 pdf 数据更改为 int。

【问题讨论】:

  • pdf.save()之前添加await

标签: flutter dart


【解决方案1】:

正如@Nishuthan 所指出的,我们应该使用await 关键字来解析Future,并通过以下方式将Uint8List 转换为List&lt;int&gt;

  Future savePdf() async {
    Directory documentDirectory = await getApplicationDocumentsDirectory();
    String documentPath = documentDirectory.path;
    File receiptFile = File("$documentPath/receipt.pdf");
    receiptFile.writeAsBytesSync(List.from(await pdf.save()));
  }

【讨论】:

    【解决方案2】:

    pdf.save()之前添加await

     receiptFile.writeAsBytesSync(await pdf.save());
    

    【讨论】:

      猜你喜欢
      • 2020-12-03
      • 2020-03-22
      • 1970-01-01
      • 1970-01-01
      • 2021-08-30
      • 1970-01-01
      • 2021-07-10
      • 2020-10-11
      • 2021-01-05
      相关资源
      最近更新 更多