Home > database >  convert a list of strings to list of type dynamic
convert a list of strings to list of type dynamic

Time:05-12

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

CodePudding user response:

Please use the map operator

final dropDownValues=["Running","Climbing","Walking"];

final data = dropDownValues.map((el) => {"display": el,"value" el,}).toList();
  • Related