Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 从颤振资源加载.pdf文件/自定义文件_Flutter - Fatal编程技术网

Flutter 从颤振资源加载.pdf文件/自定义文件

Flutter 从颤振资源加载.pdf文件/自定义文件,flutter,Flutter,我的问题是,在部署flatter app时,是否有办法在app资源中加载.pdf文件,如assets?我想使用以下方式显示PDF文件: 但无需从internet加载;) 将资源复制到临时文件 Future<File> copyAsset() async { Directory tempDir = await getTemporaryDirectory(); String tempPath = tempDir.path; File tempFile = F

我的问题是,在部署flatter app时,是否有办法在app资源中加载.pdf文件,如assets?我想使用以下方式显示PDF文件:


但无需从internet加载;)

将资源复制到临时文件

  Future<File> copyAsset() async {
    Directory tempDir = await getTemporaryDirectory();
    String tempPath = tempDir.path;
    File tempFile = File('$tempPath/copy.pdf');
    ByteData bd = await rootBundle.load('assets/asset.pdf');
    await tempFile.writeAsBytes(bd.buffer.asUint8List(), flush: true);
    return tempFile;
  }
Future copyAsset()异步{
目录tempDir=await getTemporaryDirectory();
字符串tempPath=tempDir.path;
File tempFile=File(“$tempPath/copy.pdf”);
ByteData bd=await rootBundle.load('assets/asset.pdf');
wait tempFile.writeAsBytes(bd.buffer.asUint8List(),flush:true);
返回临时文件;
}

对于那些想知道getTemporaryDirectory()从何而来的人来说,你需要一个库:如何在web上实现这一点?@HajoLemcke浏览器无法写入本地计算机,甚至连临时文件夹也不行。如果您试图在浏览器中打开PDF,您可以从服务器(为您的Flitter web应用提供服务的服务器)提供该PDF,并使用
url\u launcher
打开带有PDF的新窗口。抱歉。我指的是任何文件,不是专门的pdf文件。我可以将一个文件放入/lib/assets/config.json,然后只想加载它。这在web中是否应该是可能的?@HajoLemcke它只是正常工作(但不要把它放在
lib
)。将其放入
assets/config.json
,添加到
pubspec.yaml
中,然后使用,例如,
rootBundle.loadString('assets/config.json')。然后(打印)如果不起作用,请提出一个新问题,并在此处随意标记。