downloadStream static method

Future<void> downloadStream({
  1. required Stream<int> stream,
  2. required String filename,
  3. bool saveToDocuments = false,
})

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();
  }
}