resolveBytes method
Writes in-memory document bytes to a temporary file and returns its
path.
The native document loaders take a file URI/URL, so opening a document
from a Uint8List goes through a temp file. The file is cached under
cacheKey (so a widget rebuild reuses it) and removed by release /
clearAll, exactly like a resolved asset.
Implementation
Future<String> resolveBytes(
Uint8List bytes, {
required String cacheKey,
}) async {
if (_resolvedPaths.containsKey(cacheKey)) {
return _resolvedPaths[cacheKey]!;
}
final tempDir = await getTemporaryDirectory();
// Derive the filename from the (unique) cache key itself rather than its
// hashCode, so two different keys can't collide onto the same temp file
// and clobber each other's bytes. Sanitize non-filename-safe characters.
final safeName = cacheKey.replaceAll(RegExp(r'[^A-Za-z0-9_-]'), '_');
final tempFile = File('${tempDir.path}/nutrient_bytes_$safeName.pdf');
await tempFile.writeAsBytes(bytes, flush: true);
final resolvedPath = tempFile.path;
_resolvedPaths[cacheKey] = resolvedPath;
return resolvedPath;
}