'?,flutter,dart"/>

首页 > 解决方案 > 为什么我收到 Flutter HTTP 包错误类型“字符串”不是“地图”类型的子类型'?

问题描述

我正在使用 Flutter 的 HTTP 包的确切代码示例,但正文内容不同,如下所示:

res = await http.post(
  Uri.parse("http://localhost:3000/login"),
  headers: <String, String>{
    'Content-Type': 'application/json; charset=UTF-8',
  },
  body: jsonEncode(<String, String>{
    'email': "test@test.com",
    "password": "testPassword",
  }),
);

然而,我收到了type 'String' is not a subtype of type 'Map<String, dynamic>'正文格式的错误。而且我不能在没有编码的情况下离开身体,Map<String, String>因为这会使 API 失败。

我应该怎么办?

标签: flutterdart

解决方案


jsonEncode 输出一个字符串。您只需将地图发送到您的 API。


推荐阅读