I always try not to post useless questions in the stack thread and I try to google first but I haven't found a solution yet. so I open a question.
My Framework is Flutter and I have used the following library :
dropdown_button2: ^1.9.0
The problem is that my Custom DropdownButton2 program crashes and as far as I can tell the return value to the list is null, I don't know after the selected item in DropdownButton2, I deleted it make a crash.
so briefly
crashed happens in my app when I delete a selected item on DropdownButton2 and add another item to the hive.
CustomDropdownButton2(
value: selectedValue,
icon: Icon(
Icons.keyboard_arrow_down,
color: Colors.white,
),
iconSize: 20,
hint: context.localeString('message_device_choose'),
dropdownItems: items,
buttonDecoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: Colors.blueAccent,
),
onChanged: (value) {
setState(() {
int index = items.indexOf(value.toString());
final device = box!.getAt(index) as Devices;
prefManage.setSelectedPhonePref(device.phone);
selectedPhone = device.phone;
selectedValue = value.toString();
prefManage.setSelectedNamePref(value.toString());
});
},
),
After back add_screen to main_screen list is refreshed
void refreshDevices() {
items.clear();
for (int i = 0; i < box!.length; i++) {
final device = box!.getAt(i) as Devices;
items.add(device.name);
}
setState(() {});
}
I tried to set the default value to dropdown until fix this problem and I think my problem is the selected value but I can't find the solution to fix it.
I know the problem but I don't know idea to fix it, a problem is when I delete the selected item on CustomDropdownButton2 so finally selectedValue in CustomDropdownButton2 is null and crashes
As the error suggests, the variable items
is empty. Try to debug whether it goes inside the for loop or not. Because before the for loop, you are clearing the list and if it didn't go inside the loop, the items
list will be empty which is causing the error.