Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

261
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda