dart - 如何获取资产中的文件路径以进行颤动
问题描述
我想在资产中打开一个pdf文件,所以我需要获取文件路径。
这是我的解决方案,我不得不复制文件,因为我无法获得路径。我认为最好的解决方案是直接获取文件地址。
Future<void> writeToFile(ByteData data, String path) {
final buffer = data.buffer;
return new File(path).writeAsBytes(
buffer.asUint8List(data.offsetInBytes, data.lengthInBytes));
}
Future<File> createFileOfPdfUrl() async {
final filename = 'test.pdf';
var bytes = await rootBundle.load("assets/data/test.pdf");
String dir = (await getApplicationDocumentsDirectory()).path;
writeToFile(bytes,'$dir/$filename');
File file = new File('$dir/$filename');
return file;
}
解决方案
为了能够使用资产,您需要在pubspec.yaml上声明路径,如Flutter docs about assets and images中所述,如下所示:
flutter:
assets:
- assets/
如果取决于您的实现,使用文件,在不了解更多信息的情况下,我可以指示您遵循阅读和写作官方文档。
推荐阅读
- node.js - 在外部使用 answer .then () 并在节点 js 的另一部分代码中使用
- angular - 使用 APP_INITIALIZER 时如何使用 forRoot 导入模块?
- git - 错误:RPC 失败;结果 = 56,HTTP 代码 = 0
- ejabberd - ejabberd 社区版是否支持rest API
- html - 带数字的简单输入
- python - 每次我在 Python 中调用函数时改变不同的参数
- sql - 为什么在这里使用复杂的 DB2 "VALUE" 函数?
- oracle - Oracle APEX 自动在区域中添加/删除列
- android - Android Room getById(id: Int) 查询从第一个片段调用时返回一个有效对象,但从所有其他片段调用时返回 null
- hibernate - Declare NamedStoredProcedureQuery without Entity