swapController method

void swapController(
  1. String fieldName,
  2. FieldBindingController controller, {
  3. FlutterWidgetBinder? binder,
})

Swaps out a field's binding controller with a new one while preserving its current value.

This method allows dynamically replacing how a field is handled at runtime by switching to a different controller implementation. The current value of the field is preserved during the swap.

Implementation

void swapController(
  String fieldName,
  FieldBindingController controller, {
  FlutterWidgetBinder? binder,
}) {
  final index = _fieldNames.indexOf(fieldName);
  if (index == -1) {
    throw ArgumentError("No field with name $fieldName");
  }
  if (binder != null) {
    factories[index] = binder;
  }
  final currentValue = fields[index].getValue();
  fields[index] = controller;
  controller.setValue(currentValue);
}