'How to get response from streamed response in flutter? [duplicate]
I am developing an app using flutter and I am using http library to call the api I built.
and I want to make a multipart request to send files and it sends it as well but I can't receive any response from the server because the object returned is StreamResponse.
please tell me how to get the body of the response.
Code Snippet:
var request = http.MultipartRequest('POST', Uri.parse('$SERVER_URL/signup'));
request.files.add(await http.MultipartFile.fromPath(
'image',
user.image,
filename: 'image',
contentType: MediaType('multipart/form-data', 'multipart/form-data'),
));
StreamResponse x = await request.send();
//get body of the response
Thanks,
Solution 1:[1]
Just use http.Response.fromStream()
import 'package:http/http.dart' as http;
var streamedResponse = await request.send();
var response = await http.Response.fromStream(streamedResponse);
Solution 2:[2]
StreamedResponse has a stream getter as you'd expect that delivers a stream of byte arrays.
Assuming that you want characters instead of bytes, push those through the appropriate character decoder - let's assume UTF-8.
You then might want all those joined into a single string, so we can use join. Giving you:
print(await x.stream.transform(utf8.decoder).join());
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Richard Heap |
