Remove dropdown item from dropdown once selected

44 views Asked by At

I am implementing a DropDownButtonFormField() which contains a list of items.

 final dropDownItems = repairIssues!
    .map((e) => DropdownMenuItem<String>(
        child: Text(
          e,
          style: theme.largeSubtitleTextStyle,
        ),
        value: e))
    .toList();

DropdownButtonHideUnderline(
  child: DropdownButtonFormField<String>(
    autovalidateMode: AutovalidateMode.onUserInteraction,
    decoration: InputDecoration(
      contentPadding: EdgeInsets.symmetric(horizontal: 12, vertical: 12),
      border: OutlineInputBorder(
        borderSide: BorderSide(width: 1),
        borderRadius: BorderRadius.circular(8),
      ),
      isDense: true,
    ),
    hint: Text(
      PgaLocalization().msg()!.selectAnIssue,
      style: theme.largeSubtitleTextStyle,
    ),
    value: null, <-- the value is set null always
    isDense: true,
    onChanged: (value) {
      
      provider.addRepairIssue(value!); <--- addind item in separate array
      
    },
    validator: (value) {
      if (provider.arrSelectedRepairIssues.isEmpty) {
        return 'Please select an issue to continue.';
      }

      return null;
    },
    items: dropDownItems,
  ),
);

The addRepairIssue() is my provider class.

List<String>? _repairIssues; <--- getting data from server
List<String>? get repairIssues => _repairIssues;

List<RepairIssue> _arrSelectedRepairIssues = [];
List<RepairIssue> get arrSelectedRepairIssues => _arrSelectedRepairIssues;
addRepairIssue(String val) {
  _arrSelectedRepairIssues.add(val);
  _repairIssues!.removeWhere((element) => element != val);
  notifyListeners();
}

When user select any item from the list...

  • That should be removed from the dropdown item
  • I am storing selected item separately in an array.

so when i try to remove the item from the drop down item it gives me following error.

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════

The following assertion was thrown building Builder(dirty, dependencies: [_FocusInheritedScope]):
There should be exactly one item with [DropdownButtonFormField]'s value: String.
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value
'package:flutter/src/material/dropdown.dart':
Failed assertion: line 976 pos 15: 'items == null || items.isEmpty || value == null ||
              items.where((DropdownMenuItem<T> item) {
                return item.value == value;
              }).length == 1'

I know that the value of the dropdown should be either null or the value from the items;

In the above code, you can see that I am keeping value: null always.

How can I fix this error?

Or is there any other widget in Flutter by which I can achieve the above case?

Thanks in advance.

0

There are 0 answers