Sahid rahutomo
Sahid rahutomo

Reputation: 90

How to make Custom List in Flutter?

Hello i want to make list for spinner, its value is made with for looping int starting from 1945 until the present year, i've already know how to get the current year and list.

var now = DateTime.now();
print(DateFormat('yyyy').format(now));

But, i confuse how to make looping in the list. i've try to make for looping but its forbidden me to make it in State,

String dropdowntahun = '2020';
List<String> spinnertahun = [
      ];

This is my spinner

                             DropdownButton<String>(
                                value: dropdowntahun,
                                icon: Icon(Icons.arrow_drop_down_circle),
                                iconSize: 24,
                                elevation: 16,
                                style: TextStyle(color: Colors.red, fontSize: 18),
                                underline: Container(
                                  height: 2,
                                  color: Colors.deepPurpleAccent,
                                ),
                                onChanged: (String data) {
                                  setState(() {
                                    dropdowntahun= data;
                                  });
                                },
                                items: spinnertahun.map<DropdownMenuItem<String>>((String value) {
                                  return DropdownMenuItem<String>(
                                    value: value,
                                    child: Text(value),
                                  );
                                }).toList(),
                              ),

in java the code like this

for(int a=1945; a<=int.parse();a++){
   spinnertahun.add(a);
}

how & where do I supposed to make it? Can anyone help me?

Upvotes: 1

Views: 164

Answers (1)

ambiguous58
ambiguous58

Reputation: 1411

final int currentYear = DateTime.now().year;

final List<int> myList = [for (int i = 1945; i < currentYear + 1; i++) i];
print(myList);

This is known as a collection for.

Upvotes: 1

Related Questions