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

标签 dart

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

相关文章:

asp.net - 如何从 Visual Studio 运行 Dart 代码?

Flutter 将 Draggable Scrollbar 添加到 CustomScrollView?它一直给我 'The argument type CustomScrollView can' t 被分配给 BoxScrollView'

flutter - Flutter从父亲那里触发小部件内部的函数

file - 如何使用Flutter同步(不等待)从Assets文件夹中读取文件

flutter - 如何在 Tab 的 BoxDecoration 中添加边距?

generics - 在Dart泛型中使用动态

dart - 如何快速测试 TypeMirror 是否是另一个的子类型

flutter - 如何使用我的整个应用程序在 TabBarView 中创建可滚动的内容?

flutter - 有没有办法在 Flutter 中以小写字母显示文本?

dart - Firestore batch.commit一次添加500多个文档