0

I need to collect a link of this format : fd=1&td=2&test%5Btime%5D=10. I have an array

Map<String, dynamic> jsonTest = {
      'time': '10',
    };

Map<String, dynamic> jsonMap = {
      'fd': '1',
      'td': '1',
      'test': '$jsonTest'
    };

When I try to build a query:

var request = Uri.parse("$link/api")
        .resolveUri(Uri(queryParameters: jsonMap));

I get this query: fd=1&td=1&test=%7Btime%3A+10%7D in request.query;

How do I get a query in the form I described at the beginning of my question, thank you all in advance for your answers

1 Answer 1

1

To get the output you want, jsonMap will need to have a Map entry that looks like:

'test[time]': '10'

Assuming that jsonTest's keys are variable, you will need to iterate over jsonTest and add appropriate entries to jsonMap yourself. Dart's collection-for construct can do this succinctly:

Map<String, dynamic> jsonTest = {
  'time': '10',
};

Map<String, dynamic> jsonMap = {
  'fd': '1',
  'td': '1',
  for (var entry in jsonTest.entries) 'test[${entry.key}]': entry.value,
};
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much, it really helped, do I understand correctly that I can add other keys to jsonTest?
Yes, you can add other 'KEY': 'VALUE' entries to jsonTest if you want each of them to result in a query component that looks like test%5BKEY%5D=VALUE.

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.