flutterfuturedropdownbutton

Future<DropDownButton> items empty/null/same values | Flutter


During the process of my web-application I want the user to allow to make changes and save them. For that process I'm using SharedPreferences in order to store the changes. I have a list of titles called konzernDataTitle. In general this list is used to display my titles.

In order to change something I edit this list and "upload" it to my SharedPreferences. However, everything works fine but I cant get the new list prefsKonzernTitle into my DropDownButton. For that I'm using a FutureBuilder. The error is quite simple:

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
[...]
The following assertion was thrown building FutureBuilder<List<DropdownMenuItem<String>>>(dirty,
state: _FutureBuilderState<List<DropdownMenuItem<String>>>#dcca3):
Assertion failed:
items == null || items.isEmpty || value == null ||
              items.where((DropdownMenuItem<T> item) {
                return item.value == value;
              }).length == 1
"There should be exactly one item with [DropdownButton]'s value: MyTitle. \nEither zero or 2 or more
[DropdownMenuItem]s were detected with the same value"

The relevant error-causing widget was:
  FutureBuilder<List<DropdownMenuItem<String>>>

FutureBuilder Function:

  Future<List<DropdownMenuItem<String>>> getDropDownItems() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    bool prefsTitleExist;
    var newList = List<String>.empty();
    if (prefs.containsKey("prefsKonzernTitle")) {
      var getPrefList = prefs.getStringList("prefsKonzernTitle");
      newList = getPrefList!;
      prefsTitleExist = true;
    } else {
      prefsTitleExist = false;
    }
    final actualList = prefsTitleExist ? newList : konzernDataTitle;
    return actualList.map((data) {
      return DropdownMenuItem<String>(
        value: data,
        child: Text(
          data,
        ),
      );
    }).toList();
  }

FutureBuilder Widget


FutureBuilder<List<DropdownMenuItem<String>>>(
                            future: getDropDownItems(),
                            builder: (context, snapshot) {
                              if (!snapshot.hasData) {
                                return const SizedBox();
                              }
                              return DropdownButton<String>(
                                value: dropdownValue,
                                items: snapshot.data,
                                onChanged: (String? newValue) {
                                  setState(() {
                                    dropdownValue = newValue!;
                                    i = konzernDataTitle.indexOf(newValue);
                                    titleController.text = konzernDataTitle[i];
                                    linkController.text = konzernDataLink[i];
                                    imageController.text = konzernDataImage[i];
                                    colorController.text =
                                        konzernDataColor[i].toString();
                                  });
                                },
                              );
                            }),


I searched the problems inside the lists but all lists are exactly how they have to be. So maybe you can help me out. Thanks for any kind of help. All questions will be answered in a few minutes.

^Tim


Solution

  • There should be exactly one item with [DropdownButton]'s value: MyTitle.
    Either zero or 2 or more [DropdownMenuItem]s were detected with the same value

    The above error denotes dropdownValue does not match with any value available in dropdown menu item list or more than one value present in dropMenuItemList.

    For the first case set dropdownValue = null on initialization and for the second case check menu item doesn't have duplication.