downloadWithProgress method
- @Deprecated('Use downloadToFile() for large files to avoid memory exhaustion')
Download a file with progress callback (legacy in-memory version).
WARNING: This method accumulates the entire file in memory. For large files, use downloadToFile instead to stream directly to disk and avoid memory exhaustion on iOS.
Returns the downloaded bytes.
onProgress is called with (bytesReceived, totalBytes).
Implementation
@Deprecated('Use downloadToFile() for large files to avoid memory exhaustion')
Future<List<int>> downloadWithProgress(
String url, {
void Function(int received, int total)? onProgress,
}) async {
final request = http.Request('GET', Uri.parse(url));
final response = await _client.send(request);
if (response.statusCode != 200) {
throw Exception('Download failed: HTTP ${response.statusCode}');
}
final contentLength = response.contentLength ?? 0;
final bytes = <int>[];
int received = 0;
await for (final chunk in response.stream) {
bytes.addAll(chunk);
received += chunk.length;
onProgress?.call(received, contentLength);
}
return bytes;
}