loadStructuredData<T> method

  1. @override
Future<T> loadStructuredData <T>(String key, Future<T> parser(String value))

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.


Future<T> loadStructuredData<T>(String key, Future<T> parser(String value)) {
  assert(key != null);
  assert(parser != null);
  if (_structuredDataCache.containsKey(key))
    return _structuredDataCache[key];
  Completer<T> completer;
  Future<T> result;
  loadString(key, cache: false).then<T>(parser).then<void>((T value) {
    result = new 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.
  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 = new Completer<T>();
  _structuredDataCache[key] = completer.future;
  return completer.future;