'Flutter: setState not updating value
In my code the setState is not updating the value that show in the Dropdownbutton, I already try two configurations, as I had read that this has something to do with the build process of the widget, but none of the two methods works
Code 1
class InteriorDropdownButton extends StatefulWidget {
const InteriorDropdownButton({Key? key}) : super(key: key);
@override
_InteriorDropdownButtonState createState() => _InteriorDropdownButtonState();
}
class _InteriorDropdownButtonState extends State<InteriorDropdownButton> {
final interiorTypeList = [
'Cuero',
'Foamizada',
'Vinilo',
'Tela',
'Microfibra'
];
String? selectedInterior;
DropdownButton<String> androidDropdown() {
List<DropdownMenuItem<String>> dropdownItems = [];
for (String interiorType in interiorTypeList) {
var newItem = DropdownMenuItem(
child: Text(
interiorType,
style: const TextStyle(
fontWeight: FontWeight.w400,
color: Colors.black,
fontSize: 20,
),
),
value: interiorType,
);
dropdownItems.add(newItem);
}
return DropdownButton<String>(
items: dropdownItems,
value: selectedInterior,
icon: const Icon(Icons.expand_more_outlined),
iconSize: MediaQuery.of(context).size.height * 0.04,
isExpanded: true,
hint: const Text('Tapiceria'),
style: TextStyle(
fontWeight: FontWeight.w400,
color: Colors.black,
fontSize: MediaQuery.of(context).size.height * .025,
),
onChanged: (value) {
setState(() {
selectedInterior = value;
});
print('Hola ${selectedInterior}');
print('Hello ${value}');
});
}
CupertinoPicker iOSPicker() {
List<Text> pickerItems = [];
for (String interiorType in interiorTypeList) {
pickerItems.add(Text(interiorType));
}
return CupertinoPicker(
diameterRatio: 1,
itemExtent: 32.0,
onSelectedItemChanged: (selectedIndex) {
setState(() {
selectedInterior = interiorTypeList[selectedIndex];
});
},
children: pickerItems,
);
}
@override
Widget build(BuildContext context) {
return androidDropdown();
}
}
Code 2
class MoldsDropdownButton extends StatefulWidget {
const MoldsDropdownButton({Key? key}) : super(key: key);
@override
_MoldsDropdownButtonState createState() => _MoldsDropdownButtonState();
}
class _MoldsDropdownButtonState extends State<MoldsDropdownButton> {
String? dropdownValue;
@override
Widget build(BuildContext context) {
final moldsTypeList = [
'Madera/Cromo/Plastico',
'Madera/Plastico',
'Plastico'
];
DropdownButton<String> androidDropdown() {
List<DropdownMenuItem<String>> dropdownItems = [];
for (String moldsType in moldsTypeList) {
var newItem = DropdownMenuItem(
child: Text(moldsType),
value: moldsType,
);
dropdownItems.add(newItem);
}
return DropdownButton<String>(
value: dropdownValue,
items: dropdownItems,
icon: const Icon(Icons.expand_more_outlined),
iconSize: MediaQuery.of(context).size.height * 0.04,
isExpanded: true,
hint: const Text('Molduras'),
style: TextStyle(
fontWeight: FontWeight.w400,
color: Colors.black,
fontSize: MediaQuery.of(context).size.height * .025,
),
onChanged: (value) {
setState(() {
dropdownValue = value;
});
});
}
CupertinoPicker iOSPicker() {
List<Text> pickerItems = [];
for (String interiorType in moldsTypeList) {
pickerItems.add(Text(interiorType));
}
return CupertinoPicker(
diameterRatio: 1,
itemExtent: 32.0,
onSelectedItemChanged: (selectedIndex) {
setState(() {
dropdownValue = moldsTypeList[selectedIndex];
});
},
children: pickerItems,
);
}
return androidDropdown();
}
}
the console prints are: flutter: Hola null flutter: Hello Foamizada
The value is updated, but the selectedInterior prints null
Solution 1:[1]
both are working fine... use flutter clean or restart the code editor and emulator.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | imran sifat |
