loadUriText function
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 throughfileCache(or constructs one usingcacheDirandclient) 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();
}
}