title: Flutter http streamedResponse.stream and streamedResponse.sink
body: <p>I'm learning the third example of Flutter http package, this is the base of the code: <a href="https://pub.dev/packages/http" rel="nofollow noreferrer">https://pub.dev/packages/http</a></p>
  <blockquote>
  <p>When the request is sent via BaseClient.send, only the headers and whatever data has already been written to <strong>StreamedRequest.stream</strong> will be sent immediately. More data will be sent as soon as it's written to <strong>StreamedRequest.sink</strong>, and when the sink is closed the request will end.</p>
  </blockquote>
  <p><a href="https://pub.dev/documentation/http/latest/http/StreamedRequest-class.html" rel="nofollow noreferrer">https://pub.dev/documentation/http/latest/http/StreamedRequest-class.html</a></p>
  <ol>
  <li><p>From the docs, I don't understand how we should write to <code>StreamedRequest.stream</code>? (To send data immediately)</p>
  </li>
  <li><p>Isn't StreamedResponse.sink basically where we add our HTTP POST's Request Body: Why does it only accept a <code>List&lt;int&gt;</code>? Shouldn't it be a <code>Map&lt;String, String&gt;</code>? If it's not then where should we add the request body? <code>NEW:</code> Even when I encode it with ut8.encode, it still doesn't show up on Fiddler's WebForms when I'm debugging, how do I send a x-www-form-urlencoded properly?:</p>
  </li>
  </ol>
  <p>Code:</p>
  <pre><code>userAgentClient = UserAgentClient(userAgent, client);
  streamedRequest = http.StreamedRequest('POST', Uri(scheme: 'http', path: '/posts/', host: 'jsonplaceholder.typicode.com'));
  streamedRequest.sink.add([123, 456]); // It has to be a List&lt;int&gt;
  
  //NEW:
  streamedRequest.sink.add(utf8.encode('username=123&amp;password=456'));
  streamedRequest.sink.add(utf8.encode('{&quot;username&quot;:&quot;123&quot;,&quot;password&quot;:&quot;456&quot;}'));
  </code></pre>
  <ol start="3">
  <li>Why do I have to close the sink to be able to access StreamedResponse's properties?</li>
  </ol>
  <p><code>streamedRequest.sink.close();</code></p>
  <p><strong>Update:</strong></p>
  <pre><code>class UserAgentClient extends http.BaseClient {
    final String userAgent;
    final http.Client client;
    UserAgentClient(this.userAgent, this.client);
    Future&lt;http.StreamedResponse&gt; send(http.BaseRequest request){
      request.headers['user-agent'] = userAgent;
      return client.send(request);
    }
  }
  
  dynamic _status = '';
  dynamic _body = '';
  dynamic _headers = '';
  String _reason = '';
  http.Client client = http.Client();
  String userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36';
  UserAgentClient userAgentClient;
  http.StreamedRequest streamedRequest;
  
  void _httpStreamed(){
    userAgentClient = UserAgentClient(userAgent, client);
    streamedRequest = http.StreamedRequest('POST', Uri(scheme: 'http', path: '/posts/', host: 'jsonplaceholder.typicode.com'));
    streamedRequest.sink.add(utf8.encode('{&quot;username&quot;:&quot;123&quot;,&quot;password&quot;:&quot;456&quot;}'));
    setState(() {
      _status = streamedRequest.url;
      _body = '';
      _headers = '';
      _reason = '';
    });
  }
  
  void _httpSend() async{
      http.StreamedResponse streamedResponse;
      streamedResponse = await userAgentClient.send(streamedRequest);
      streamedResponse.stream.listen(
        (value) async{
            _body = http.ByteStream.fromBytes(value);
            _body = await _body.bytesToString();
        },
        onError: (e, sT) {
          SnackBar sBar = SnackBar(content: Text('$e\n$sT',));
          Scaffold.of(context).showSnackBar(sBar);
        },
        onDone: () {
          SnackBar sBar = SnackBar(content: Text('Done lol'),);
          Scaffold.of(context).showSnackBar(sBar);
        },
      );
      setState(() {
        _body;
        _status = streamedResponse.statusCode;
        _headers = streamedResponse.headers;
        _reason = streamedResponse.reasonPhrase;
      });
    }
  }
  
  void _httpClose(){
    if (streamedRequest != null){
      streamedRequest.sink.close();
    }
  }
  </code></pre>
  <p>So I run the first 2 functions but the <code>_body</code> variable doesn't show up on my screen until I run the <code>_httpClose()</code> function.</p>