insertAsyncDropDownItem<T> method
void
insertAsyncDropDownItem<T>(})
Inserts a new value into the items list of an AsyncDropDownFieldState.
This method allows dynamically adding a new item to the dropdown field, which is useful for supporting inline creation or external injection of values.
fieldName: The name of the async dropdown field to update.value: The new item to insert into the dropdown list.index: The position in the list where the new item should be inserted (default is0, i.e., top of the list).
Implementation
void insertAsyncDropDownItem<T>(
String fieldName, {
required T value,
required String label,
int index = 0,
}) {
final field = state.asyncDropDownField<T>(fieldName);
final cached = List<DropDownItemState<T>>.from(field.cachedItems ?? []);
cached.insert(index, DropDownItemState<T>(value, label: label));
state.fields[fieldName] =
field.copyWith(cachedItems: cached, items: Future.value(cached));
state = state.copyWith(fields: Map.from(state.fields));
}