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

206
Views
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 answers
Answer question

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 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!