0

I'm back with another issue with the DropdownButton.

The DropdownButton is not enabled. I found this in api.flutter.dev

If the onChanged callback is null or the list of items is null then the dropdown button will be disabled, i.e. its arrow will be displayed in grey and it will not respond to input.

Here is my code now:

return new DropdownButton<String>(
                            hint: new Text("Select Agency"),
                            value: _currentAgency,
                            onChanged: changedDropDownAgency,
                            items: snapshot.data.docs.forEach((document) {
                              return new DropdownMenuItem<String>(
                                value: document.data()['name'],
                                child: new Text(document.data()['name']),
                              );
                            }),
                          );

void changedDropDownAgency(String selected_agency) {
    setState(() {
      _currentAgency = selected_agency;
    });
    globals.selectedAgency = selected_agency;
  }

The forEach loop runs fine and in debug mode I can see that there is data in the document object. I don't know how to debug the DropdownButton code to see why the button is not active. Any suggestions would be helpful. Thanks.

1 Answer 1

1

forEach() on Iterables does not return any value (see: https://api.dart.dev/stable/2.10.5/dart-core/Iterable/forEach.html), and thus items is null and the DropdownButton is disabled. Use map instead (https://api.dart.dev/stable/2.10.5/dart-core/Iterable/map.html). Example:

snapshot.data.docs.map<DropdownMenuItem<String>>((document) {
    return new DropdownMenuItem<String>(
        value: document.data()['name'],
        child: new Text(document.data()['name']),
    );
}).toList(),
Sign up to request clarification or add additional context in comments.

10 Comments

thank you for your response. However, when I use your code I get the following: The following _TypeError was thrown building StreamBuilder<QuerySnapshot>(dirty, state: _StreamBuilderBaseState<QuerySnapshot, AsyncSnapshot<QuerySnapshot>>#5bb78): type 'List<dynamic>' is not a subtype of type 'List<DropdownMenuItem<String>>?'
I've edited my answer, please see if this works. I am explicitly telling dart what the return type of Map should be.
The following assertion was thrown building StreamBuilder<QuerySnapshot>(dirty, state: _StreamBuilderBaseState<QuerySnapshot, AsyncSnapshot<QuerySnapshot>>#b9a5e): There should be exactly one item with [DropdownButton]'s value: Select Agency. Either zero or 2 or more [DropdownMenuItem]s were detected with the same value 'package:flutter/src/material/dropdown.dart': Failed assertion: line 855 pos 15: 'items == null || items.isEmpty || value == null || items.where((DropdownMenuItem<T> item) { return item.value == value; }).length == 1'
return new DropdownButton<String>( //hint: new Text("Select Agency"), value: _currentAgency, onChanged: changedDropDownAgency, items: snapshot.data.docs .map<DropdownMenuItem<String>>((document) { return new DropdownMenuItem<String>( value: document.data()['name'], child: new Text(document.data()['name']), ); }).toList(), );
_currentAgency seems to have the value of 'Select Agency' and none or 2 or more of your DropdownMenuItems seem to have this value. You should use hint instead and set the initial value (so _currentAgency) to null.
|

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.