dart

How to convert to List<String> to Map<String,String> in Dart?


I have List<String> = ["name","class","age"];

I want this list to be converted to Map<String,String>;

Like Map<String,String>["name":"name","class":"class","age":"age"];

How to achieve that?


Solution

  • I had this before. Here is how I do it.

    List<String> list = ["name", "class", "age"];
    
    // Convert the list to a map
    Map<String, String> map = Map.fromIterable(
      list,
      key: (item) => item,
      value: (item) => item,
    );
    
    // Print the map (You can use this map in your Flutter app)
    print(map);
    
    

    Output: {name: name, class: class, age: age}

    Happy flutter !