Closed Colton127 closed 4 months ago
It's a matter of race condition in your code.
When you do selectedValueListenable.value = widget.value;
, it will trigger the internal listener (which updates the selected index) and that listener will be using the old items as DropdownButton2 is not built with the new items yet "[build] is being called after [didUpdateWidget]".
You should instead do:
@override
void didUpdateWidget(DropDownWidget oldWidget) {
if (oldWidget.value != widget.value) {
selectedValueListenable.dispose();
selectedValueListenable = ValueNotifier<int>(widget.value);
}
super.didUpdateWidget(oldWidget);
}
@override
void didUpdateWidget(DropDownWidget oldWidget) {
if (oldWidget.items != widget.items) {
selectedValueListenable.dispose();
selectedValueListenable = ValueNotifier<int>(widget.value);
} else if (oldWidget.value != widget.value) {
selectedValueListenable.value = widget.value;
}
super.didUpdateWidget(oldWidget);
}
is also valid
If you update both items and selected value simultaneously, whereby the new items do not contain the previously selected value, the following error is thrown:
See reproducible example below. It appears that dropdown_button is rebuilt with the new items before the ValueNotifier is updated with the new value.
Output:
selecting value: 65601. items: [350816, 127516, 289448, 65601, 376364, 295008, 451829, 219180, 496107, 317415]
Selected value is contained within item list values despite error being thrown.
Temporary fix is to create a new ValueNotifier every time items are updated:
Thank you