Dart URI:具有相同键的多个查询参数

Dart URI: multiple query parameters with the same key

我正在使用 Mapquest directions API,我的目标是在一堆地图点(3 个或更多)之间创建一条路线。

这是 "to" 参数 of the endpoint that's used to fetch a route for a given set of points 的 API 规范(它对应于目标):

When the input format is key/value pairs, the ending location(s) of a Route Request. More than one to parameter may be supplied. This is used for single-line addresses only.

Refer to the Locations documentation on how to properly form locations in all formats, including JSON and XML.

AFAIK 这意味着可以为查询指定多个 "to" 参数,例如“http://www.mapquestapi.com/directions/v2/route?from=40.65,73.95&to=40.87,-73.85&to=40.70,-73.56。我说得对吗?

所以假设我正确理解规范,我需要构建一个适当的 URI 实例,该实例将被分派到 http 的库 get 方法。问题是它在构造函数中接收了一个Map<String, String个查询参数,map表示唯一性。如何使用 URI class?

构建这样的请求

我试图通过仅检索 URI 实例的字符串表示而不指定 to 键值对并仅编写串联的 to- s 在字符串的末尾,随后传递给 http.get。我错过的是 URI 负责字符的转义,当 SDK 已经为我提供 URI class.[=25 时,我自己写这样的东西似乎是完全不合适的=]

我不知道你的理解是否正确,但我可以告诉你如何多次创建具有相同查询参数的 URI。

Uri.parse 函数确实不止一次接受相同的名称,但您正在尝试创建字符串,所以这没有用

要从原始数据创建 URI,构造函数 Uri 有一个 queryParameters 参数,它接受一个 Map<String, dynamic>,其中值必须是单个 String 或一个 Iterable<String>。所以,在你的情况下:

var request = Uri.parse("http://www.mapquestapi.com/directions/v2/route")
    .resolveUri(Uri(queryParameters: {
      "from": "40.65,73.95",  
      "to": ["40.87,-73.85", "40.70,-73.56"],
    }));

如果您有一个查询部分多次包含相同的名称,那么您需要在 Uri class 到 read[=30= 上使用特殊函数] 那些查询参数。默认 queryParameters getter returns a Map<String, String> 每个名字只能有一次。您可以改用 queryParametersAll getter。