For in loop in Flutter Dart – Example Code

For in loop is used in flutter to loop through a list of items. Here is the sample code looping thought list of currency and generating dropdown list item menu for each of it.

const List<String> currenciesList = [
  'AUD',
  'BRL',
  'CAD',
  'CNY',
  'EUR',
  'GBP',
  'HKD',
  'IDR',
  'ILS',
  'INR',
  'JPY',
  'MXN',
  'NOK',
  'NZD',
  'PLN',
  'RON',
  'RUB',
  'SEK',
  'SGD',
  'USD',
  'ZAR'
];


  List<DropdownMenuItem> getCurrencyList(){
    List<DropdownMenuItem<String>> dropdownItems = [];

    for( var item in currenciesList){
      String currency = item;
      var newItem = DropdownMenuItem(
        child: Text(currency),
        value: currency,
      );
      dropdownItems.add(newItem);
    };

    return dropdownItems;
  }

About the Author: smartcoder

You might like

Leave a Reply

Your email address will not be published. Required fields are marked *