简体   繁体   中英

Dropdownbutton state management, Flutter

I coded a dropdownbutton so it gets the items from a database from back4app, it shows the user the items that he saved on said database. But when I try to hold the state for the selection of said item, it shows a blank dropdownbutton, eventhough it is showing the items from the database. Here is the error: [A value of type 'String' can't be assigned to a variable of type 'List'. Try changing the type of the variable, or casting the right-hand type to 'List']. What did I do wrong?

DropdownButton<String>(
              items: dropdownItems.map((ParseObject value) {
                return DropdownMenuItem<String>(
                  value: value.get<String>('numeracao')!,
                  child: Text(value.get<String>('numeracao')!),
                );
              }).toList(),
              onChanged: (String? newValue) {
                setState(() {
                  dropdownItems = newValue!;
                });
              },
            ),

You need a new var to hold the current value. Let's say String dropdownValue . Then your DropownButton should look something like this:

DropdownButton<String>(
              value: dropdownValue,
              items: dropdownItems.map((ParseObject value) {
                return DropdownMenuItem<String>(
                  value: value.get<String>('numeracao')!,
                  child: Text(value.get<String>('numeracao')!),
                );
              }).toList(),
              onChanged: (String? newValue) {
                setState(() {
                  dropdownValue = newValue!;
                });
              },
            ),

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