getText method

Future<String> getText(
  1. Uri uri, {
  2. Duration? ttl,
  3. bool forceRefresh = false,
})

Fetches plain text (UTF-8 string) from a Uri.

Supports file, http, and https schemes. Throws UnsupportedError for any unsupported scheme.

Implementation

Future<String> getText(
  Uri uri, {
  Duration? ttl,
  bool forceRefresh = false,
}) async {
  switch (uri.scheme.toLowerCase()) {
    case 'file':
      return File.fromUri(uri).readAsString();

    case 'http':
    case 'https':
      final file = await getFile(
        uri.toString(),
        ttl: ttl,
        forceRefresh: forceRefresh,
      );
      if (file == null) {
        throw HttpException('Failed to load remote text from $uri', uri: uri);
      }
      return file.readAsString();

    default:
      throw UnsupportedError('Unsupported URI scheme: ${uri.scheme}');
  }
}