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

205
Vistas
Convert JavaScript String to Map Flutter

I have a string like this:

'var key1 = "abcd"; var key2 ="xyz";'

and I want to convert this into the map or JSON, in Dart, like this:

{
'key1': 'abcd',
'key2': 'xyz'
}

Is there a way I can convert the J/S file as a string to a Dart map?

about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

To generate a Map form your string. You can use the help of regular expressions (online demo).

The following code captures two groups that are within the () and creates a Map out of them:

void main() {
  final s = 'var key1 = "abcd"; var key2 ="xyz"';
  
  final reg = RegExp(r'var (.*?)=\s?"(.*?)"');
  final myMap = Map.fromEntries(
      reg.allMatches(s).map((m) => MapEntry(m.group(1), m.group(2))));
  print(myMap);
}

Output:

{key1 : abcd, key2 : xyz}
about 4 years ago · Santiago Gelvez 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