我想转换一个File
to a ByteData
颤动中的物体。
像这样的东西:
import 'dart:io';
File file = getSomeCorrectFile(); //This file is correct
ByteData bytes = ByteData(file.readAsBytesSync()); //Doesnt compile
return bytes;
我明白ByteData
构造函数接收字节数的长度并用 0 初始化它们,所以我可以做类似的事情ByteData(file.readAsBytesStync().length);
但那我该如何填充它们呢?
我缺少什么?
在 Dart 2.5.0 或更高版本中,我相信以下内容应该有效:
import 'dart:io';
import 'dart:typed_data';
...
File file = getSomeCorrectFile();
Uint8List bytes = file.readAsBytesSync();
return ByteData.view(bytes.buffer);
(在 Dart 2.5.0 之前,file.readAsBytesSync()
行应该是:
Uint8List bytes = file.readAsBytesSync() as Uint8List;
File.readAsBytes https://api.dartlang.org/stable/2.3.0/dart-io/File/readAsBytes.html/File.readAsBytesSync https://api.dartlang.org/stable/2.3.0/dart-io/File/readAsBytesSync.html用于声明返回一个List<int>
, but 返回的对象实际上是一个Uint8List subtype https://github.com/dart-lang/sdk/issues/31547.)
一旦你有了字节作为Uint8List
, 你可以提取其ByteBuffer https://api.dartlang.org/stable/2.7.1/dart-typed_data/TypedData/buffer.html并构建一个ByteData
从那。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)