简体   繁体   中英

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM