Dropdownbutton state management, Flutter

117 Views Asked by At

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!;
                });
              },
            ),
1

There are 1 best solutions below

0
On

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!;
                });
              },
            ),