loadStructuredData<T> method
Null safety
override
Retrieve a string from the asset bundle, parse it with the given function, and return the function's result.
The result of parsing the string is cached (the string itself is not,
unless you also fetch it with loadString). For any given key
, the
parser
is only run the first time.
Once the value has been parsed, the future returned by this function for subsequent calls will be a SynchronousFuture, which resolves its callback synchronously.
Implementation
@override
Future<T> loadStructuredData<T>(String key, Future<T> Function(String value) parser) {
assert(key != null);
assert(parser != null);
if (_structuredDataCache.containsKey(key)) {
return _structuredDataCache[key]! as Future<T>;
}
Completer<T>? completer;
Future<T>? result;
loadString(key, cache: false).then<T>(parser).then<void>((T value) {
result = SynchronousFuture<T>(value);
_structuredDataCache[key] = result!;
if (completer != null) {
// We already returned from the loadStructuredData function, which means
// we are in the asynchronous mode. Pass the value to the completer. The
// completer's future is what we returned.
completer.complete(value);
}
});
if (result != null) {
// The code above ran synchronously, and came up with an answer.
// Return the SynchronousFuture that we created above.
return result!;
}
// The code above hasn't yet run its "then" handler yet. Let's prepare a
// completer for it to use when it does run.
completer = Completer<T>();
_structuredDataCache[key] = completer.future;
return completer.future;
}