0

This is a dropdown list I have. How do I change the code to have a condition where if the user chooses an option it will give "valueChoose_f4" that chosen option else give "valueChoose_f4" the value that is stored in "M${widget.orgUnit}". Thanks, hope my question made sense.

ListTileTheme(
                      tileColor: Colors.white,
                      child: ListTile(
                        title: Container(
                          decoration: BoxDecoration(
                            color: Colors.transparent,
                            border: Border.all(color: Colors.blueGrey),
                            borderRadius: BorderRadius.circular(10),
                          ),
                          child: Padding(
                            padding: const EdgeInsets.all(8.0),
                            child: DropdownButtonHideUnderline(
                              child: DropdownButton<String>(
                                isExpanded: true,
                                value: valueChoose_f4,
                                hint: Text('M${widget.orgUnit}'),
                                onChanged: (newValue){
                                  setState(() {
                                    valueChoose_f4 = newValue;
                                  });
                                },
                                items: listUserType.map((map) {
                                  return DropdownMenuItem<String>(
                                    value: map['value'],
                                    child: Text(map['name']),
                                  );
                                }).toList(),
                              ),
                            ),
                          ),
                        ),
                      ),
                    ),

1 Answer 1

1

You can't give a dropdown value that is not contained in your listUserType so if you want to set a default value where the dropdown initializes and not has been selected yet you can do it like:

value: valueChoose_f4 ?? 'Your_default_value'

that means when valueChoose_f4 is null, the default value will be instead the null.

String? valueChoose_f4;

ListTile(
        title: Container(
          decoration: BoxDecoration(
            color: Colors.transparent,
            border: Border.all(color: Colors.blueGrey),
            borderRadius: BorderRadius.circular(10),
          ),
          child: Padding(
            padding: const EdgeInsets.all(8.0),
            child: DropdownButtonHideUnderline(
              child: DropdownButton<String>(
                isExpanded: true,
                value: valueChoose_f4 ?? listUserType.first['value'], // changed
                hint: const Text('M${widget.orgUnit}'),
                onChanged: (newValue){
                  setState(() {
                    valueChoose_f4 = newValue;
                  });
                },
                items: listUserType.map((map) {
                  return DropdownMenuItem<String>(
                    value: map['value'],
                    child: Text(map['name']),
                  );
                }).toList(),
              ),
            ),
          ),
        ),
      )

I hope this solve your problem.

Sign up to request clarification or add additional context in comments.

2 Comments

it works, but there's an error at "String? valueChoose_f4;" The error is : "Null safety features are disabled for this library." Is there any other way of doing it without upgrading my language version. Displaying the default value works, but it is not being stored into "valueChoose_f4"
if you using old flutter sdk verison with unsound null safety then remove ? and you will be fine like: String valueChoose_f4;

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.