Possible to update/rebuild open DropdownButtonFormField?

Viewed 21

Is it possible to uppdate or rebuild open DropdownButtonFormField, e.g. with GetIt or Provider - or perhaps by using a stream - when new data becomes available?

At the moment the options in the dropdown gett added as data arrives while the dropdown is un-expanded, but once the user taps on it only those items that were available at the time they tapped are available and new items are only available if and when the user closes and reopens the dropdown.

My user interface currently has

DropdownButtonFormField(
                      value: dropdownValue,
                      icon: const Icon(Icons.keyboard_arrow_down),
                      items: dropdownItems,
                      validator: (value) =>
                          (value == null || value == "0") ? 'Please select a recipient.' : null,
                      onChanged: (String? newValue) {
                        if (newValue != null) {
                          dropdownValue = newValue;
                        } else {
                          dropdownValue = "0";
                        }
                        setState(() {});
                      },
                    )

the dropdownItems comes from


List<DropdownMenuItem<String>>? get dropdownItems {
          if (clientListicle.items.isEmpty) {
      developer.log('go fetch', name: '_MessagePageState get dropdownItems');
      fetchClients();
    }
    for (var element in clientListicle.items) {
      DropdownMenuItem<String> potentialItem =
          DropdownMenuItem(value: element.id.toString(), child: Text(element.title));
      bool isHave = false;
      for (var exstElement in menuItems) {
        if (exstElement.value == element.id.toString()) {
          isHave = true;
        }
      }
      if (!isHave) {
        menuItems.add(potentialItem);
        setState(() {});
      }
    }
    if (menuItems.length == 1) {
      return null;
    } else {
      return menuItems;
    }
  }

clientListicle is a singleton that inherits from a class that extends ChangeNotifier, and is registered in GetIt.

I've had a quick look at the implementation of DropdownButtonFormField and FormField that it extends, thinking maybe one could add functionality to or override a build method or some such, but think maybe I'm missing something simpler/easier and am probably just a bit out of my depth here... :-)

Update, I've tried adding a final _formFieldKey = GlobalKey<FormFieldState>(); key to the dropdown widget thinking I might be able to use that from the getter to trigger a rebuild, but no luck yet.

0 Answers
Related