Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

251
Views
Is there any method to convert string to map<int, dynamic>?

I make an API from Laravel and check the 'Get' request from Postman where it will return as follow:

"answer_selected": "{1:\"True\",2:\"False\"}"

Then, the Flutter application will read the JSON and serialize it using the model class.

Are there any ways to convert the value of "answer_selected" to map<int, dynamic>? Or, my JSON API response format is incorrect?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You should not return a string that is not a complete JSON. Either return

  • {"answer_selected": {1:"True",2:"False"}}

or

  • {1:"True",2:"False"}

will be much better. jsonDecode in dart supports decode to map<String, dynamic, and the key in JSON must be string too. So the JSON should like

  • {"1":"True","2":"False"}

If you want to transform it to map<int, dynamic>, just do

  void test() {
    String text = '{"1":"True","2":"False"}';
    Map<String, dynamic> map = jsonDecode(text);
    Map<int, dynamic> desiredMap =
        map.map((key, value) => MapEntry(int.parse(key), value));
    desiredMap.entries.forEach((element) {
      print('${element.key} ${element.value}');
    });
  }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!