Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

flutter - How to get response body with request.send() in dart

I'm doing an api request uploading an image with

var request = new http.MultipartRequest("POST", uri);
var response = await request.send()

in dart using flutter. I can then check the response code with for instance

if (response.statusCode == 200) {
   print('ok');
}

with other calls I can also get the response body with

var result = response.body;

however when using request.send() I can't seem to find out how to get the response body result.

Any help or input is very much appreciated, thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I checked the docs for request.send I returns Future<StreamedResponse> instead of Future<Response>

Digging more for StreamedResponse I found that it response.stream which is a ByteStream

Here is what you can do to get response in String

final response = await request.send();
final respStr = await response.stream.bytesToString();

In my opinoin you should only use request.send if you want streamed response instead of "collected" response. More about streams in dart here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...