title: Flutter http large file download and save on disk
body: <p>I'm trying to save a large file from network to disk (160MB file). I'm saving the chunks as soon as they arrive but my saved file on disk is always corrupted and having variable size each time I re-run the script.</p>
  <p>The reason I'm trying to save it as soon as a chunk has arrived is to save memory. I tried fetching the entire file and then save it, which caused my flutter to consume about 2GB of memory.</p>
  <p>This is my code:</p>
  <pre><code>final request = http.Request('GET', uri);
  final streamedResponse = await request.send();
  File pathToSave = File('C:\\Downloads\\test.zip');
  
  streamedResponse.stream.listen((value) async
  {
      await pathToSave.writeAsBytes(value, mode: FileMode.writeOnlyAppend, flush: true);
  })
  
  </code></pre>
  <p>I'm not sure but it seems the listen function doesn't wait for the previous callback to get finished and fires the next callback immediately and meddles with the previous file write.
  Same script works fine with small files.</p>
  <p>Flutter 3.0.5 (desktop)
  Windows 11</p>