uri_content 4.0.1
uri_content: ^4.0.1 copied to clipboard
Get the Uint8List content of a given Uri. Supports file, data, content and http/https
uri_content #
Get Uint8List content from a Uri in Flutter.
This package reads content directly from the original source and streams bytes in memory. It does not create temporary files while fetching content.
Supported URI schemes #
data:file:http:https:content:(Android only)
Usage styles #
You can use uri_content in two ways:
Uriextension methods (getContent()/getContentOrNull()).- A
UriContentinstance (recommended for testability, injection, custom headers, and Androidcontent:buffer tuning).
Quick start #
import 'dart:typed_data';
import 'package:uri_content/uri_content.dart';
Future<Uint8List> loadBytes() async {
final uri = Uri.parse('https://example.com/file.bin');
try {
return await uri.getContent(); // extension on Uri
} catch (e) {
// getContent() can throw when content can't be retrieved.
rethrow;
}
}
If you prefer nullable behavior on failure:
final bytesOrNull = await Uri.parse('https://example.com/file.bin').getContentOrNull();
Using UriContent directly #
Use UriContent when you want dependency injection/mocking, custom headers, a custom HttpClient, or control over Android content: read buffer size.
import 'dart:io';
import 'package:uri_content/uri_content.dart';
final uriContent = UriContent(
defaultHttpHeaders: {'Authorization': 'Bearer <token>'},
httpClient: HttpClient(),
);
final bytes = await uriContent.from(
Uri.parse('https://example.com/private.bin'),
httpHeaders: {'X-Request-Id': 'abc-123'},
);
Streaming large content #
from() returns all bytes in a single Uint8List. For large files, prefer getContentStream() to process chunks and reduce memory usage. Fetching very large content with from() may use too much memory and can crash your app.
import 'package:uri_content/uri_content.dart';
final uri = Uri.parse('https://example.com/large-file.zip');
final stream = UriContent().getContentStream(uri);
await for (final chunk in stream) {
// process/save chunk
}
If you stop early, cancel the stream subscription. Always consume the stream to completion or cancel it to avoid resource leaks.
For Android content: URIs, bufferSize controls chunk size (default: 5 MB). Larger values can considerably increase performance, while smaller values reduce memory consumption:
final stream = UriContent().getContentStream(
Uri.parse('content://...'),
bufferSize: 1024 * 1024, // 1 MB
);
Utility methods #
final uriContent = UriContent();
final uri = Uri.parse('https://example.com/file.bin');
final canFetch = await uriContent.canFetchContent(uri);
final length = await uriContent.getContentLength(uri); // may be null
final lengthOrNull = await uriContent.getContentLengthOrNull(uri);
canFetchContent()checks whether content can be fetched (for example, file existence forfile:URIs and reachability forhttp/httpsURIs).getContentLength()uses metadata and may be inaccurate; it can throw if content is not reachable; it returnsnullwhen length is unavailable.getContentLengthOrNull()returnsnullon errors, butnullis ambiguous (unreachable content vs unavailable length).
Errors #
from()andgetContent()throw when content cannot be retrieved.fromOrNull()andgetContentOrNull()returnnullon errors.- Unsupported schemes throw
UnsupportedSchemeError.