Reputation: 4375
I'm using Mapquest directions API and my goal is to create a route between a bunch of map points (3 and more).
Here is the API spec for "to" parameter of the endpoint that's used to fetch a route for a given set of points (it corresponds to the destination):
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 it means that multiple "to" parameters can be specified for a query, e.g. "http://www.mapquestapi.com/directions/v2/route?from=40.65,73.95&to=40.87,-73.85&to=40.70,-73.56 . Am I right about this?
So assuming that I understood the spec right, I need to construct an appropriate URI
instance that will be dispatched to the http's library get
method. The problem is that it receives a Map<String, String
of query parameters in the constructor, and map means uniqueness. How can I construct such a request using URI
class?
I've tried to make a workaround by just retrieving a string representation of an URI
instance without specifying the to
key-value pair and just writing the concatenated to
-s in the end of the string with subsequent passing to the http.get
. What I missed is that URI
takes care for characters' escaping, and writing such a thing by myself seems to be completely inappropriate when the SDK already provides me with URI
class.
Upvotes: 3
Views: 3501
Reputation: 71773
I do not know if your reading is correct, but I can say how to create a URI with the same query parameter more than once.
The Uri.parse
function does accept the same name more than once, but you are trying to create the string, so that's not useful
To create the URI from raw data, the constructor Uri
has a queryParameters
argument which accepts a Map<String, dynamic>
where the value must be either a single String
or an Iterable<String>
. So, in your case:
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"],
}));
If you have a query part which contains the same name more than once, then you need to use special functions on the Uri
class to read those query parameters. The default queryParameters
getter returns a Map<String, String>
which can only have each name once. You can use the queryParametersAll
getter instead.
Upvotes: 10