0

This is my code, and i dont know why the hint doesnt change, when i click the options, obviously all this code is inside a class, I tried to use a for loop to identify the list positions , and if they were right, it would appear on the screen.

final listgenre = ["Masc", "Fem", "Não Binário"];
var listgenre_value;
String newValue = "";
var valueChoose;
String hintValue = "";
   Padding(
                padding: EdgeInsets.all(16),
                child: Container(
                    padding: EdgeInsets.only(left: 16, top: 10, right: 15),
                    decoration: BoxDecoration(
                        border: Border.all(color: Colors.grey, width: 2),
                        borderRadius: BorderRadius.circular(20)),
                    child: DropdownButton<String>(
                      hint: Text("Genero :$hintValue"),
                      dropdownColor: Colors.white,
                      icon: Icon(Icons.arrow_drop_down),
                      iconSize: 36,
                      iconEnabledColor: Colors.black,
                      isExpanded: true,
                      style: TextStyle(fontSize: 17, color: Colors.black),
                      value: valueChoose,
                      underline: SizedBox(
                        width: 320,
                        height: 200,
                      ),
                      items: listgenre.map((valueitem) {
                        return DropdownMenuItem(
                          value: valueitem,
                          child: Text(valueitem),
                        );
                      }).toList(),
                      onChanged: (newValue) {
                        setState(() {
                          for (int i = 0; i <= listgenre.length; i++) {
                            if (listgenre[i] != newValue) {
                              listgenre_value = i + 1;
                            } else {
                              hintValue = "$newValue";
                              // ignore: void_checks
                              return listgenre_value;
                            }
                          }

                          Object? valueChoose = newValue;
                          String valueChoosen = valueChoose.toString();
                        });
                      },
                    ))),
  • What is your expected output? I ran the above code and [this](https://i.ibb.co/XDWzc52/Flutter.jpg) is what I got. – abdev Jul 10 '21 at 21:42
  • Well, i really expected that output, but on my phone the "Masc" part just doesnt appear. Thanks for the help. Did u change anything in the code? – Gabriel-Ms Jul 10 '21 at 22:08
  • Not really. Did you try running it on multiple emulators? – abdev Jul 11 '21 at 10:37
  • No, actually because my visual studio cant run any of those(with the android emulators), and i tried that a lot of times, every and each time i run my app it's on my real phone. an LM X540 – Gabriel-Ms Jul 11 '21 at 16:22
  • Does it work with the solution now? Because it is the only issue I think is causing the undesired output. – abdev Jul 14 '21 at 10:41
  • Yes, it worked, just putting my variables and arrays declared before "Widget Build" , thanks for the help – Gabriel-Ms Jul 18 '21 at 15:46

2 Answers2

1

As per your comments, the same code doesn't give the expected output.

It is only possible if you are also initialising the variables inside the build method, which shouldn't be the case.

Incorrect Code

@override
  Widget build(BuildContext context) {
    
    // ---- your variables INSIDE build method

    final listgenre = ["Masc", "Fem", "Não Binário"];
    var listgenre_value;
    String newValue = "";
    var valueChoose;
    String hintValue = "";

// ------- rest of the code continued

Correct Code

  final listgenre = ["Masc", "Fem", "Não Binário"];
  var listgenre_value;
  String newValue = "";
  var valueChoose;
  String hintValue = "";

  @override
  Widget build(BuildContext context) {

  // ------- rest of the code continued

As the build method is called every time setState is called, hintValue was being initialised to "" i.e empty string.

abdev
  • 597
  • 4
  • 17
0

The correct way to fix this error is abdev's comment. It will help if you put the variables used by the DropdownButton widget above the build method. I hope it was helpful!