addChoice method

List<MultiselectOption> addChoice(
  1. MultiselectOption choice,
  2. bool? multiSelect
)

Implementation

List<MultiselectOption> addChoice(
    MultiselectOption choice, bool? multiSelect) {
  List<MultiselectOption> newValues = multiSelect == true ? [...state] : [];

  // Remove the choice if it already exists.
  if (newValues.firstWhereOrNull(
          (MultiselectOption option) => option.value == choice.value) !=
      null) {
    newValues = newValues.where((val) => val.value != choice.value).toList();
  } else {
    newValues.add(choice);
  }

  state = newValues;
  return newValues;
}