title: Flutter - How to download a file from server using binary stream
body: <p>I need to be able to download and display an image from a private server. The request that I send needs to include a header with content-type and a body with sessionToken and userId. 
  The server responds with a binary stream with the Content-type application/octet-stream. </p>
  
  <p>This is the code I have right now: </p>
  
  <pre><code> Future&lt;Null&gt; _downloadFile(String url, String userId, sessionToken) async {
      Map map = {'token': sessionToken, 'UserId': userId};
  
      try {
        var request = await httpClient.getUrl(Uri.parse(url));
        request.headers.set('content-type', 'application/json');
        request.add(utf8.encode(json.encode(map)));
        var response = await request.close();
        var bytes = await consolidateHttpClientResponseBytes(response);
        await _image.writeAsBytes(bytes);
        userImage(_image);
      }
      catch (value){
        print(value);
      }
  
    }
  </code></pre>
  
  <p>When I try to read the response I get this error: 
  HttpException: Content size exceeds specified contentLength. 72 bytes written while expected 0. </p>
  
  <p>I've tried to google this endlessly on how to download a file from a server using a stream, but I can't find anything. What I need is something similar to the <a href="https://docs.microsoft.com/en-us/dotnet/api/system.drawing.bitmap?redirectedfrom=MSDN&amp;view=netframework-4.7.2" rel="noreferrer">bitmap class in .NET</a> that can take in a stream and turn it into an image. </p>
  
  <p>Can anybody help me? It would be greatly appreciated. </p>