首页 > 解决方案 > pdf没有被加载

问题描述

我在颤振中使用 pdf 包并尝试使用以下方法加载图像:

final image = PdfImage.file(
    pdf.document,
    bytes: File('assets/logo.pdf').readAsBytesSync(),
  );

之后,我将它插入到 Image 类中:

 Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: <Widget> [
                Image(image)
              ]
            ),

但它给出了这个错误: Unhandled Exception: FileSystemException: Cannot open file, path = 'assets/logo.pdf' (OS Error: No such file or directory, errno = 2)

为什么会这样,我无法加载pdf?

标签: flutterdart

解决方案


资产与文件不同。您不能以相同的方式访问他们的数据。您当前正在尝试assets/logo.pdf设备文件系统访问,而不是从您的应用程序资产,因此它预计不存在。

要获取资产数据,您需要使用rootBundlewithload方法。

final image = PdfImage.file(
  pdf.document,
  bytes: (await rootBundle.load('assets/logo.pdf')).buffer.asUint8List(),
);

推荐阅读