如何从颤振中的流式响应中获取响应?

Bug*_*Bug 3 api http dart flutter

我正在使用 flutter 开发一个应用程序,并使用 http 库来调用我构建的 api。

我想发出一个多部分请求来发送文件,它也发送它,但我无法从服务器收到任何响应,因为返回的对象是 StreamResponse。

请告诉我如何获取回复的正文。

代码片段:

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


Run Code Online (Sandbox Code Playgroud)

谢谢,

Abd*_*rek 11

只需使用 http.Response.fromStream()

import 'package:http/http.dart' as http;

var streamedResponse = await request.send();
var response = await http.Response.fromStream(streamedResponse);
Run Code Online (Sandbox Code Playgroud)