作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Flutter 将“ Assets ”加载到文件
中,以便 native 应用程序可以访问它。
这是我加载资源的方式:
final dbBytes = await rootBundle.load('assets/file');
这将返回 ByteData
的实例。
如何将其写入 dart.io.File
实例?
最佳答案
ByteData
是一个抽象:
A fixed-length, random-access sequence of bytes that also provides random and unaligned access to the fixed-width integers and floating point numbers represented by those bytes.
正如 Gunter 在评论中提到的,您可以使用 File.writeAsBytes
。它确实需要一些 API 工作才能从 ByteData
获取到 List<int>
然而。
import 'dart:async';
import 'dart:io';
import 'dart:typed_data';
Future<void> writeToFile(ByteData data, String path) {
final buffer = data.buffer;
return new File(path).writeAsBytes(
buffer.asUint8List(data.offsetInBytes, data.lengthInBytes));
}
我也 filed an issue使 Flutter 文档对于此用例更加清晰。
关于dart - 如何在 Dart 中将 `ByteData` 实例写入文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50119676/
我是一名优秀的程序员,十分优秀!