1

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.

6
  • It seems you are actually adding it not to a separate list but to the ones that make up the items of your dropdown, but it's hard to judge without seeing the code. The error lies most likely in addRepairIssue Commented Jan 30, 2024 at 14:05
  • addRepairIssue method just add an item in array. It is independent array and nothing to do it with items showing in dropdown. Commented Jan 30, 2024 at 14:18
  • added addRepairIssue() code. Commented Jan 30, 2024 at 14:22
  • I would think you would want to do _repairIssues!.removeWhere((element) => element == val);, so == instead of !=. Although I don't think it explains the bug, but who knows, maybe changing that helps Commented Jan 30, 2024 at 14:29
  • I need to remove the item which is being added to another error. That's why == is used to remove that selected item in dropdown. Commented Jan 30, 2024 at 14:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.