【发布时间】:2018-12-20 02:10:25
【问题描述】:
在颤动中,rootBundle.load() 给了我一个ByteData 对象。
dart 中的 ByteData 对象到底是什么?可以用来异步读取文件吗?
我真的不明白这背后的动机。
为什么不给我一个好的File 对象,或者更好的是资产的完整路径?
就我而言,我想从资产文件中异步读取字节,逐字节地写入新文件。 (构建一个不会挂断 UI 的 XOR 解密)
这是我能做的最好的事情了,但它可悲地挂断了 UI。
loadEncryptedPdf(fileName, secretKey, cacheDir) async {
final lenSecretKey = secretKey.length;
final encryptedByteData = await rootBundle.load('assets/$fileName');
final outputFilePath = cacheDir + '/' + fileName;
final outputFile = File(outputFilePath);
if (!await outputFile.exists()) {
Stream decrypter() async* {
// read bits from encryptedByteData, and stream the xor inverted bits
for (var index = 0; index < encryptedByteData.lengthInBytes; index++)
yield encryptedByteData.getUint8(index) ^
secretKey.codeUnitAt(index % lenSecretKey);
print('done!');
}
print('decrypting $fileName using $secretKey ..');
await outputFile.openWrite(encoding: AsciiCodec()).addStream(decrypter());
print('finished');
}
return outputFilePath;
}
【问题讨论】:
标签: asynchronous file-io dart flutter