【问题标题】:type '_InternalLinkedHashMap<String, String>' is not a subtype of type 'Iterable<dynamic>' [Solved]type '_InternalLinkedHashMap<String, String>' 不是 'Iterable<dynamic>' 类型的子类型 [已解决]
【发布时间】:2019-10-17 09:06:54
【问题描述】:

我正在使用包 http 创建一个发布请求。 我的代码是

final params = {
        'cartItem': {
          'sku': product.sku,
          'qty': '1',
          'quote_id': "xxxxx",
          'price': product.price.toString()
        }
      };
      final body = jsonEncode(params);
      var uri = Uri.parse(ClientConfigs.loadBasicURL()+APIPath.guestCartsPath+quoteID+"/items");
      uri = uri.replace(queryParameters: params);
      final response = await http.post(uri, headers: {'Authorization': 'Bearer ' + ClientConfigs.accessToken}, body: body);

它打破了一个例外, type '_InternalLinkedHashMap&lt;String, String&gt;' is not a subtype of type 'Iterable&lt;dynamic&gt;' 它指向 uri.dart 类中的这段代码:

queryParameters.forEach((key, value) {
      if (value == null || value is String) {
        writeParameter(key, value);
      } else {
        Iterable values = value; // Here is the broken point
        for (String value in values) {
          writeParameter(key, value);
        }
      }
    });

所以我的问题是如何使用像我这样的嵌套主体发出帖子请求。 感谢您的帮助。

更新:感谢@ChennaReddy 问题是我添加的uri = uri.replace(queryParameters: params); 对queryParameters 无效。 而且我注意到我需要将“”更改为“”,例如:“cartItem”而不是“cartItem”,我不确定 Dart 的编码,但这样更改会使请求生效。

【问题讨论】:

  • 您能否补充一下,您尝试使用这些查询参数调用/实现的最终 URL 是什么?
  • 那你为什么需要uri = uri.replace(queryParameters: params);?无论如何,您是按照自己的意愿将它们发送到身体中吗?
  • 你是对的@ChennaReddy。非常感谢。

标签: http post flutter nested uri


【解决方案1】:

由于这一行而发生此错误:

uri = uri.replace(queryParameters: params);

这样做的目的是将uri参数替换为新的,这里是一个例子:

var uri = Uri.parse("http://www.example.com/?q=test");
final replaceParams = {"q":"othertest"};
uri = uri.replace(queryParameters: replaceParams);
print(uri);

输出将是:

I/flutter ( 6060): http://www.example.com/?q=othertest

所以,这些是GET 请求的参数。


在您的情况下,您将uri.replace()GET 参数混淆并在那里传递POST 参数。因此,您似乎不需要调用 uri.replace() 方法,这将解决此错误。

【讨论】:

  • 谢谢!当我通过@Chenna Reddy 的评论立即检测到这一点时,它就解决了。
  • 太好了,我回答是为了解释问题以及如何使用uri.replace(),以防有人尝试使用它并遇到同样的问题
猜你喜欢
  • 2021-06-19
  • 2020-03-04
  • 2019-05-02
  • 2019-01-03
  • 2019-01-26
  • 2020-11-25
  • 1970-01-01
  • 2022-08-09
  • 2021-01-22
相关资源
最近更新 更多