我正在尝试使用以下方法加载一个大的ish (1000行,68k)文本文件
final String enString = await rootBundle.loadString('res/string/string_en.json');加载字符串的Dart类函数AssetBundle.loadString是
Future<String> loadString(String key, { bool cache = true }) async {
final ByteData data = await load(key);
if (data == null)
throw FlutterError('Unable to load asset: $key');
// 50 KB of data should take 2-3 ms to parse on a Moto G4, and about 400 μs
// on a Pixel 4.
if (data.lengthInBytes < 50 * 1024) {
return utf8.decode(data.buffer.asUint8List());
}
// For strings larger than 50 KB, run the computation in an isolate to
// avoid causing main thread jank.
return compute(_utf8decode, data, debugLabel: 'UTF8 decode for "$key"');
}查看上面的代码,如果文件大于50k (与我的文件一样),则使用隔离。
作为一个测试,我将我的文件切成两半(如此32k),并在一秒钟内加载它(而不是使用隔离)。但是,未经编辑,函数在使用隔离时挂起。
我的文件只是一个简单的密钥-值对的json文件。以下是前几行
{
"ctaButtonConfirm": "Confirm",
"ctaButtonContinue": "Continue",
"ctaButtonReview": "Review",
"balance": "Balance",
"totalBalance": "Total Balance",
"transactions": "Transactions",
:当隔离物被使用时它会挂起来吗?
编辑
基于上面的loadString代码,我编写了一个不使用隔离的扩展函数,它工作得很好,所以它看起来不喜欢我的文件吗?
extension AssetBundleX on AssetBundle {
Future<String> loadStringWithoutIsolate(String key) async {
final ByteData data = await load(key);
return utf8.decode(data.buffer.asUint8List());
}
}发布于 2022-10-17 15:12:26
您不能从衍生隔离器中访问rootBundle。所以用主隔离代替。
或在docs中
您可以尝试使用SchedulerBinding.scheduleTask。
https://stackoverflow.com/questions/71452155
复制相似问题