【问题标题】:type '_InternalLinkedHashMap<String, Object>' is not a subtype of type 'Map<String, String>' in Flutter类型 '_InternalLinkedHashMap<String, Object>' 不是 Flutter 中类型 'Map<String, String>' 的子类型
【发布时间】:2021-07-10 02:00:10
【问题描述】:

我正在尝试将列表 (List&lt;int&gt; danceId) 传递给我的 API 调用中的参数。它是一个 MultipartRequest API 调用请求。但是当我打电话给我一个错误时

type '_InternalLinkedHashMap<String, Object>' is not a subtype of type 'Map<String, String>'

这是我的 API 请求方法

  Future<http.Response> patchApiCall(String url, String token, Map param,
      String imageKey, File file) async {
    
    var responseJson;
    var uri = Uri.parse(url);
    var request = http.MultipartRequest("PATCH", uri);
    request.headers.addAll({"Authorization": token});

    request.fields.addAll(param); /// Here are comes an error


    if (file != null) {
      var img = await http.MultipartFile.fromPath(imageKey, file.path);
      request.files.add(img);
    }

    try {
      final response =
      await request.send().timeout(const Duration(seconds: 60));
      var res = await http.Response.fromStream(response);
      responseJson = res;
    } on SocketException {
      throw FetchDataException("You are not connected to internet");
    } on TimeoutException catch (e) {
      print('Time out');
      throw TimeoutException('Time out');
    }
    return responseJson;
  }

我将这个列表 List&lt;int&gt; danceId 作为参数中的数组传递

调用参数:

{first_name: Test First, last_name: Test, email: Test@test.com, date_of_birth: 2021-04-13, about: 123456789, dance_styles: [3, 2, 1]}

我也尝试"dance_styles": json.encode(danceStyleId)this,但没有运气。现在如何传递参数?

【问题讨论】:

  • 能否请您在使用响应的地方添加代码?
  • 我只显示列表的响应,但首先我无法进行 API 调用,这是我的问题。

标签: api flutter dart parameters httprequest


【解决方案1】:

您不能将List&lt;int&gt; 添加到参数映射值中。 addAll(param) 方法只接受 Map&lt;K, V&gt; 作为类型。因为 headers getter 返回一个 Map&lt;String, String&gt; 它只接受相同的类型。

您可以更改为:

// param content:
// {'first_name': 'Test First', 'last_name': 'Test', 'email': 'Test@test.com', 'date_of_birth': '2021-04-13', 'about': '123456789', 'dance_styles': '[3, 2, 1]'}

request.fields.addAll(param); // Works

注意所有键和值都是String

【讨论】:

    猜你喜欢
    • 2021-10-06
    • 2023-04-02
    • 1970-01-01
    • 2021-01-18
    • 1970-01-01
    • 2021-02-05
    • 2019-03-23
    • 2020-03-04
    • 2022-12-23
    相关资源
    最近更新 更多