loadString method

  1. @override
Future<String> loadString(
  1. String key, {
  2. bool cache = true,
})
override

Retrieve a string from the asset bundle.

Throws an exception if the asset is not found.

If the cache argument is set to false, then the data will not be cached, and reading the data may bypass the cache. This is useful if the caller is going to be doing its own caching. (It might not be cached if it's set to true either, depending on the asset bundle implementation.)

The function expects the stored string to be UTF-8-encoded as Utf8Codec will be used for decoding the string. If the string is larger than 50 KB, the decoding process is delegated to an isolate to avoid jank on the main thread.

Implementation

@override
Future<String> loadString(String key, {bool cache = true}) {
  if (cache) {
    if (_strings[key] case var cached?) return SynchronousFuture(cached);
  }
  return load(key).then((data) {
    // Decoded here rather than through `AssetBundle.loadString`, which hands
    // anything over 50KB to `compute`. That is an isolate, its result arrives
    // on the real event loop, and under fake time the real event loop turns
    // only inside `runAsync` — so a bundle of translations would load in
    // every lane except this one. Inline costs a millisecond or two.
    var value = utf8.decode(Uint8List.sublistView(data));
    if (cache) _strings[key] = value;
    return value;
  });
}