updateAsyncDropDownItem<T> method

void updateAsyncDropDownItem<T>(
  1. String fieldName, {
  2. required T value,
  3. required String label,
})

Updates an existing dropdown item in an AsyncDropDownFieldState by its value.

  • fieldName: The name of the dropdown field.
  • value: The value to search for and update.
  • label: The new label for the existing item.

If the item doesn't exist, this method does nothing.

Implementation

void updateAsyncDropDownItem<T>(
  String fieldName, {
  required T value,
  required String label,
}) {
  final field = state.asyncDropDownField<T>(fieldName);
  final cached = List<DropDownItemState<T>>.from(field.cachedItems ?? []);

  final index = cached.indexWhere((item) => item.value == value);
  if (index == -1) return; // Value not found; exit

  cached[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));
}