dartflutter

converting string to map in dart


I wanted to convert a string to map.

String value = "{first_name : fname,last_name : lname,gender : male, location : { state : state, country : country, place : place} }"

into

Map = {
first_name : fname,
last_name : lname,
gender : male,
location = {
  state : state, 
  country : country, 
  place : place
 }
}

How do I convert the string into a map<String, dynamic> where the value consists of string, int, object, and boolean?

I wanted to save the string to a file and obtain the data from the file.


Solution

  • That's not possible.

    If you can change the string to valid JSON, you can use

    import 'dart:convert';
    ...
    Map valueMap = json.decode(value);
    // or
    Map valueMap = jsonDecode(value);
    

    The string would need to look like

    {"first_name" : "fname","last_name" : "lname","gender" : "male", "location" : { "state" : "state", "country" : "country", "place" : "place"} }