loadUriText function

Future<String> loadUriText(
  1. Uri uri, {
  2. Directory? cacheDir,
  3. UrlFileCache? fileCache,
  4. Client? client,
  5. Duration? ttl,
  6. bool forceRefresh = false,
})

Companion helper function to fetch plain text (UTF-8 string) from a Uri.

Supports file, http, and https resource schemes. Any other scheme immediately throws an UnsupportedError.

  • file: directly reads local file content as text via File.fromUri.
  • http / https: fetches remote text through fileCache (or constructs one using cacheDir and client) to provide local disk caching, RFC 7234 conditional request validation (304 Not Modified), and offline fallback.

Implementation

Future<String> loadUriText(
  Uri uri, {
  Directory? cacheDir,
  UrlFileCache? fileCache,
  http.Client? client,
  Duration? ttl,
  bool forceRefresh = false,
}) async {
  if (fileCache != null) {
    return fileCache.getText(uri, ttl: ttl, forceRefresh: forceRefresh);
  }

  final cache = UrlFileCache(
    cacheDir ?? Directory.systemTemp,
    client: client ?? http.Client(),
  );

  try {
    return await cache.getText(uri, ttl: ttl, forceRefresh: forceRefresh);
  } finally {
    cache.close();
  }
}