简体   繁体   中英

flutter dropdownbutton initial value doesn't change on screen

I have dropdownbutton and items coming dynamic list thats perfectly works. i can show all list item but when i select someone my value doesn't change on the screen. just entry.first write doesn't change to which is selected. when i check on the terminal i can see the selected item so set state method works but initialvalue doesn't change.

my dropdown button

 String dropdownvalue = entry.first;  
 var items =entry;
 DropdownButton(
           
          // Initial Value
       value: dropdownvalue,
           
          // Down Arrow Icon
          icon: const Icon(Icons.keyboard_arrow_down),   
           
          // Array list of items
          items: items.map((dynamic items) {
            return DropdownMenuItem(
              value: items,
              child: Text(items),
            );
          }).toList(),
          // After selecting the desired option,it will
          // change button value to selected value
          onChanged: (dynamic newvalue) {
            setState(() {
              dropdownvalue = newvalue; > **this not working**
            print(dropdownvalue); > **it gives the newvalue**
             });
          },
        ),

It seems that your issue is related to the way how you have declared the dropdownvalue and items list.

Please check this dartpad out. Here I'm using you code with some modifications for the declaration of items list and dropdownvalue.

https://dartpad.dev/?id=33ef41ea7fa95613fe1a498e4dac4bcc

You have to work with stateFul widget and put the variable like : dropdownvalue in global variable it means under the

class _MyAppState extends State<MyApp> {
...
}

I had the same problem and solved it like that.

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