insertAsyncDropDownItem<T> method

void insertAsyncDropDownItem<T>(
  1. String fieldName, {
  2. required T value,
  3. required String label,
  4. int index = 0,
})

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 is 0, 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));
}