【发布时间】:2019-10-09 15:43:29
【问题描述】:
我将 dart 和 Flutter 用于移动应用程序。我使用我的 api 从服务器获取数据。但是我发现了一个问题,可能是它的dart core问题。
我需要将复杂的 queryParams 添加到我的 URL 中,例如
Map<String, Map<String, List<String>>>{"a": {"b": ["c","d"]}, "e": {}}
我用Uri.parse(url).replace(queryParams: myQueryParams).toString()
但是Uri.replace() 只接受Map<String, Iterable<String>> 并抛出错误
Unhandled Exception: type '_InternalLinkedHashMap<String, List<String>>' is not a subtype of type 'Iterable<dynamic>'
我找到了引发此错误的方法
static String _makeQuery(String query, int start, int end,
Map<String, dynamic /*String|Iterable<String>*/ > queryParameters) {
if (query != null) {
if (queryParameters != null) {
throw ArgumentError('Both query and queryParameters specified');
}
return _normalizeOrSubstring(query, start, end, _queryCharTable,
escapeDelimiters: true);
}
if (queryParameters == null) return null;
var result = StringBuffer();
var separator = "";
void writeParameter(String key, String value) {
result.write(separator);
separator = "&";
result.write(Uri.encodeQueryComponent(key));
if (value != null && value.isNotEmpty) {
result.write("=");
result.write(Uri.encodeQueryComponent(value));
}
}
queryParameters.forEach((key, value) {
if (value == null || value is String) {
writeParameter(key, value);
} else {
Iterable values = value;
for (String value in values) {
writeParameter(key, value);
}
}
});
return result.toString();
}
所以我的问题是 dart 中有一些方法可以将我的 queryParams 添加到 url 还是我需要自己创建它?
【问题讨论】:
-
您希望它变成什么 URL 查询字符串?
a=something&e=something_else?请填补空白。 -
@RichardHeap 类似
?options[option_volume][]=30ml&options[option_volume][]=60ml&attributes[attribute_color_shade][]=brown&attributes[attribute_producer][]=worldfamous