gpt4 book ai didi

dart - 如何在 Dart 中将 `ByteData` 实例写入文件?

转载 作者:行者123 更新时间:2023-12-03 01:00:42 27 4
gpt4 key购买 nike

我正在使用 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/

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