flutter - 如何将 Future<StreamedResponse> 转换为 Future<Response>?

标签 flutter dart

我正在尝试发送带有正文的 DELETE 请求,但 http.delete 不支持向请求添加正文。

我在网上找到了这个解决方案,但我想返回 Response 而不是 StreamedResponse。我怎样才能做到这一点?

  static Future<http.Response> deleteFavorites({Map<String, int> body}) async {
    UserRepository userRepository = UserRepository();
    String token = await userRepository.storage.read(key: 'token');
    final client = http.Client();
    var response;
    try {
      response =
          await client.send(http.Request("DELETE", Uri.parse(favoritesUrl))
            ..headers["access-token"] = token
            ..body = jsonEncode(body));
      //
    } finally {
      client.close();
    }
    return response;
  }

最佳答案

您可以使用 Response 类 fromStream 中的静态方法,它接受 StreamedResponse 并返回您想要的 Future,只需将返回更改为:

return Response.fromStream(response);

您可以查看文档 https://pub.dev/documentation/http/latest/http/Response-class.html .

希望对您有所帮助!

关于flutter - 如何将 Future<StreamedResponse> 转换为 Future<Response>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56889334/

相关文章:

flutter - 在 Flutter 中更新对象实例属性的最佳实践是什么?该实例嵌套在提供程序类的 map 中,如下所示

Flutter - 将起始高度设置为 MediaQuery 高度时,AnimatedSize 不设置动画

objective-c - Android Studio 中现有 Flutter 项目中 Swift 或 Objective-C 的原生语言设置在哪里?

flutter - 在 Flutter 中使用多个 LocalizationsDelegate

flutter - 如何处理在 flutter 中动态创建的复选框列表?

flutter -//和///在flutter中有什么区别

Flutter/Dart Shared Preferences DateTime 被转换

Dart 聚合物 : How I can update a property from a custom element by another?

android - 在 Flutter 中,如何使用 CachedNetworkImage 显示来自 Firebase 的离线图像?

flutter - 有没有一种方法可以使用pushNamed将多个索引参数传递给另一个屏幕?