Search code examples
dartdart-html

DART passing parameter to SelectElement / OptionElement


I made this as OptionElement in DART, which was fine:

   var source = new SelectElement()
             ..children.add(new OptionElement(value: 'vCode', data:'myVendor Name'));

I want to take my vendor list as call back from the server, and put then in the select option so the user can select the vendor code/name, I made the returned Map from the server as {"code":"vCode","name":"myVendor Name"}, and wanted to add them using a loop, I tried the below but did not work::

var vendor = <Map>[];   

for(int i=0;i<=number_of_vendors;i++){
     var source = new SelectElement()
             ..children.add(new OptionElement(value: vendor[i]['code'], data: vendor[i]['name']));
}

any though? thanks


Solution

  • This can't work

    var source = new SelectElement()
             ..children.add(new OptionElement(value: "$vendor['code']", data:"$vendor['name']"));
    

    you can do either

    var source = new SelectElement() ..children.add(new OptionElement(value: vendor['code'], data:vendor['name']));

    if vendor['code'] and vendor['name'] return a string or

    var source = new SelectElement() ..children.add(new OptionElement(value: "${vendor['code']}", data:"${vendor['name']}"));

    When after the $ comes a simple identifier you can omit the curly braces, if it is an expression you need to add them.

    Update - the code actually used provided by @Hasan A Yousef

    Below code should work fine.

       var vendors = <Map>[];
    

    then

           vendors=[{"code":"vCode1","name":"myVendor Name1"},{"code":"vCode2","name":"myVendor Name2"}];
    

    or

           vendors.add({"code":"vCode 1","name":"vName 1"}); 
           vendors.add({"code":"vCode 2","name":"vName 2"});
           vendors.add({"code":"vCode 3","name":"vName 3"}); 
           vendors.add({"code":"vCode 4","name":"vName 4"});
    

    followed by:

       for(int i=0;i<vendors.length;i++){
           source..children.add(new OptionElement(value: vendors[i]['code'], data: vendors[i]['name']));
       }
    

    or by:

      for(Map vendor in vendors){
        source..children.add(new OptionElement(value: vendor['code'], data: vendor['name']));
      }