flutter - 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?
解决方案
资产与文件不同。您不能以相同的方式访问他们的数据。您当前正在尝试assets/logo.pdf
从设备文件系统访问,而不是从您的应用程序资产,因此它预计不存在。
要获取资产数据,您需要使用rootBundle
withload
方法。
final image = PdfImage.file(
pdf.document,
bytes: (await rootBundle.load('assets/logo.pdf')).buffer.asUint8List(),
);
推荐阅读
- javascript - Podchaser GraphQL Api,似乎传递了一些错误的东西
- json - 用 numbs (Javascript) 减少数组的问题
- sensors - 如何访问我的健身追踪器上的心率传感器?
- javascript - 无法重新启用禁用的按钮
- python - 为什么当我在 python 上运行时我的 tkinter 界面不显示
- java - 使用java登录页面首先显示错误并且没有HTML
- amazon-web-services - Terraform 数据 aws_lambda_function.existing 抛出错误:获取 Lambda 函数(MyLambda)时出错:ResourceNotFoundException:找不到函数
- c - 从一个 fork 进程中获得 3 个 Pid?
- python - 针对此错误在 GOOGLE Colab 中安装软件包的最有效方法是什么?
- sql - Database.Log not logging appended SQL from interceptor in Entity Framework