Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

252
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda