Flutter Lists – Create, Update, Push, Loop Sample Codes

List in flutter is a collection of objects with a length. How to create a List in Flutter. How to push an item to a List. How to loop through a flutter List.

Creating List in Flutter – Dart Code

const List<String> cryptoList = [
  'BTC',
  'ETH',
  'LTC',
];

Create Empty List in Flutter

const List<String> cryptoList = [];

List Example Flutter Project Code

  DropdownButton<String> androidDropdown() {

    List<DropdownMenuItem<String>> dropdownItems = [];    // creating an empty list of dropdown menu

    for (String currency in currenciesList) {   //looping through a list
      var newItem = DropdownMenuItem(
        child: Text(currency),
        value: currency,
      );
      dropdownItems.add(newItem);   //add an item , pushing data to list in flutter
    }

    return DropdownButton<String>(
      value: selectedCurrency,
      items: dropdownItems,
      onChanged: (value) {
        setState(() {
          selectedCurrency = value;
          getData();
        });
      },
    );
  }

List class Reference: https://api.flutter.dev/flutter/dart-core/List-class.html

Related: Key-Value Pairs – Maps in Flutter Dart Sample Codes

About the Author: smartcoder

You might like

Leave a Reply

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