title: How to progressivly download data in Dart
body: <p>I am trying to download a large mp4 file using Dart. Currently I use a http get request to download the data and then I write the body bytes into a file. This way works fine, but it has a big disadvantage, namly the whole video is written into RAM before I dump it into a file.</p>
  <pre class="lang-dart prettyprint-override"><code>final resp = await http.get(Uri.parse(url));
      File file = File(targetPath);
      await file.create(recursive: true);
      await file.writeAsBytes(resp.bodyBytes);
  </code></pre>
  <p>Because the videos that will be downloaded have a duration of 90 min they are large which makes this solution inappropriate.
  My goal is to have a stream of incoming bytes which are piped into a buffer and write every 4 kB to my file.
  I tried using a StreamedResponse, like this:</p>
  <pre class="lang-dart prettyprint-override"><code>  Future&lt;void&gt; downloadResource(String url, String targetPath) async {
      File file = File(targetPath);
      final req = http.Request(&quot;GET&quot;, Uri.parse(url));
      req.send().asStream().listen(onData);
    }
  
    void onData(http.StreamedResponse event) async {
      final newBytes = event.stream.toBytes(); //Type: Future&lt;Uint8List&gt;
  
      //process incoming bytes
    }
  </code></pre>
  <p>I don't know how to get the incoming bytes, so getting the raw data that is coming in right now. I frist thought StreamedResponse would give me any access to this data, but it is basically the same as before with only Future Operations.</p>
  <p>So if you have a solution or any hint how to achive my goal, I would be very thankful for sharing!</p>