Search code examples
stringflutterdartdynamicdatasource

convert a list of strings to list of type dynamic


Im trying to use a MultiSelectFormField to create a drop down list with multiple choices.The datasource for the MultiSelectFormField is required in the below format,

                 dataSource:[
                              {
                                "display": "Running",
                                "value": "Running",
                              },
                              {
                                "display": "Climbing",
                                "value": "Climbing",
                              },
                              {
                                "display": "Walking",
                                "value": "Walking",
                              },
                            ],

but what I have is a list of type string containing the value sthat should be displayed in the drop down.My question is how can I convert

list<String> dropDownValues=["Running","Climbing","Walking"]

to the format required


Solution

  • Please use the map operator

    final dropDownValues=["Running","Climbing","Walking"];
    
    final data = dropDownValues.map((el) => {"display": el,"value" el,}).toList();