Flutter string to map string dynamic

Flutter: Convert String to Map

To convert a string to a Map object in Flutter, you can make use of the `jsonDecode` function from the `dart:convert` package. Here’s how you can do it:

    
import 'dart:convert';

void main() {
  String jsonString = '{"name":"John", "age":30, "city":"New York"}';
  
  Map myMap = jsonDecode(jsonString);
  
  print(myMap['name']); // Output: John
  print(myMap['age']); // Output: 30
  print(myMap['city']); // Output: New York
}
    
  

In the above example, we have a JSON string representing a person’s details. Using `jsonDecode`, we convert the string to a Map object where the keys are of type String and the values can be of any dynamic type.

The resulting map can be accessed using the keys. In our example, we print the values associated with the keys `name`, `age`, and `city`.

Make sure to import the `dart:convert` package to gain access to the `jsonDecode` function.

Leave a comment