downloadStream static method
Downloads a file from a stream and saves it to the given filename. Works on all platforms (web, mobile, desktop).
Implementation
static Future<void> downloadStream({
required Stream<int> stream,
required String filename,
bool saveToDocuments = false,
}) async {
if (kIsWeb) {
// Use web-specific downloader
await downloadFromStream(stream, filename);
} else {
// On native platforms, write to appropriate directory
final dir = await _getDownloadDirectory(saveToDocuments);
final file = io.File(path.join(dir.path, filename));
final sink = file.openWrite();
// Convert Stream<int> to the bytes that sink expects
await for (final byte in stream) {
sink.add([byte]);
}
await sink.close();
}
}