首页 > 解决方案 > 如何获取资产中的文件路径以进行颤动

问题描述

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

标签: dartflutter

解决方案


为了能够使用资产,您需要在pubspec.yaml上声明路径,如Flutter docs about assets and images中所述,如下所示:

flutter:
  assets:
    - assets/

如果取决于您的实现,使用文件,在不了解更多信息的情况下,我可以指示您遵循阅读和写作官方文档


推荐阅读