1

According to the documentation, it needs to follows the Form Post rules at: https://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4. When looking at that information it did not give me much to work with in terms of complex objects or maps.

Right now, If I have a list for example: Each item in the list needs to be stringified.

var params = {"list": [1,2,3]};
// needs to be stringed.
params["list"] = params["list"].map((item)=>item.toString()).toList();

Simple. Also all base items need to be a string as well

var params = {"number": 1, "boolean": true};
params = params.forEach((k,v)=> params[k].toString());

But how do we handle maps?

var params = {"map": {"a":1,"b":"foo","c":false,"d":[]}};
// ??

It seems that after testing in my app and in dart pad, you need to make sure everything is strings, so i am trying to come up with a way to effectively cover lists, maps, and maybe more complex objects for encoding.

var params = {};
params["list"] = [1,2,3];
params["number"] = 1;
params["boolean"] = true;
params["map"] = {"a":1,"b":"foo","c":false,"d":[]};
params.forEach((String key, dynamic value){
  if(value is List){
    params[key] = value.map((v)=>v.toString()).toList();
  }else if(value is Map){
    // ????
  }else{
    params[key] = value.toString();
  }
  //maybe have an additional one for custom classes, but if they are being passed around they should already have their own JSON Parsing implementations.
}

Ideally, the result of this would be passed into:

Uri myUri = new Uri(queryParameters: params);

and right now, while i solved the list issue, it doesn't like receiving maps. Part of me just wanted to stringify the map as a whole, but i wasn't not sure if there was a better way. I know that when someone accidentally stringified the array, it was not giving me: ?id=1&id=2 but instead ?id=%5B1%2C2%5D which was not correct.

1 Answer 1

2

I don't think there is any special support for maps. Query parameters itself is a map from string to string or string to list-of-strings.

Everything else need to be brought into this format first before you can pass it as query parameter.

A simple approach would be to JSON encode the map and pass the resulting string as a single query parameter.

Sign up to request clarification or add additional context in comments.

2 Comments

that's actually what i was thinking would be the end state, just encode the map and then send it on its way. I wasn't sure if there was a specific standard I should be adhering to regarding it though.. Also Great speaking with ya again Gunter. :) I dont want to tag it quite yet as the answer, as maybe there is different input into the matter.
Sure, no worries. It's entirely up to you if or when to accept an answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.