gpt4 book ai didi

dart - 如何获取 Assets 中的文件路径以进行 flutter

转载 作者:IT王子 更新时间:2023-10-29 06:58:28 52 4
gpt4 key购买 nike

我想在 Assets 中打开一个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;
}

最佳答案

为了能够使用 Assets ,您需要在 pubspec.yaml 中声明路径,如 Flutter docs about assets and images 中所述,像这样:

flutter:
assets:
- assets/

如果使用文件取决于您的实现,在不知道更多信息的情况下,我可以指示您遵循 Reading and Writing Oficial Docs .

关于dart - 如何获取 Assets 中的文件路径以进行 flutter ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54658660/

52 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com