1

I`m creating a dropdown button in flutter, but i have a problem when i use dropdown it shows the name of the first item but i want to change it(default value) to categories and idk how to do that, also i tried to use hint(as you see in codes)but it didn't work. here is my codes:

Container(
            height: pageHeight / 15,
            padding: EdgeInsets.all(20),
            child:DropdownButton(
                value: _value,
                items: const [
                  DropdownMenuItem(
                    child: Text("First Item"),
                    value: 1,
                  ),
                  DropdownMenuItem(
                    child: Text("Second Item"),
                    value: 2,
                  ),
                ],
                onChanged: (value) {
                  setState(() {
                    _value = value as int ;
                  });
                },
                hint:Text("Select item")
            ),
          )

enter image description here

I want to change this First Item to categories

Navid Shokoufeh
  • 341
  • 6
  • 21

2 Answers2

1

Make value nullable. DropdownButton can have null value and while it is null it will show hint widget.

  int? _value;
DropdownButton(
                  value: _value,
                  items: const [
                    DropdownMenuItem(
                      child: Text("First Item"),
                      value: 1,
                    ),
                    DropdownMenuItem(
                      child: Text("Second Item"),
                      value: 2,
                    ),
                  ],
                  onChanged: (value) {
                    setState(() {
                      _value = value as int;
                    });
                  },
                  hint: Text("categories"),
                ),
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
  • that's right my value was : `int? _value=1;` – Navid Shokoufeh Nov 02 '21 at 08:57
  • Yap it was having initial value as 1, that why you were always having 1st item as selected. – Md. Yeasin Sheikh Nov 02 '21 at 08:59
  • Exception occured. `Exception has occurred. _AssertionError ('package:flutter/src/material/dropdown.dart': Failed assertion: line 888 pos 15: 'items == null || items.isEmpty || value == null || items.where((DropdownMenuItem item) { return item.value == value; }).length == 1': There should be exactly one item with [DropdownButton]'s value: . Either zero or 2 or more [DropdownMenuItem]s were detected with the same value)` – Dev Conductor Aug 10 '23 at 13:41
  • 1
    @DevConductor can you share the link of your question? – Md. Yeasin Sheikh Aug 10 '23 at 17:50
  • @Md. Yeasin Sheikh. Thanks for your reply. I fixed my issue. – Dev Conductor Aug 10 '23 at 18:01
0

Just Asign on initState()

selectedDropDownValue = "categories";

Container(
            height: pageHeight / 15,
            padding: EdgeInsets.all(20),
            child:DropdownButton(
                items: const [
                  DropdownMenuItem(
                    child: Text("First Item"),
                    value: 1,
                  ),
                  DropdownMenuItem(
                    child: Text("Second Item"),
                    value: 2,
                  ),
                ],
                value: selectedDropDownValue, 
                onChanged: (value) {
                  setState(() {
                    selectedDropDownValue = value;
                  });
                },
                hint:Text("Select item")
            ),
          )
mega6382
  • 9,211
  • 17
  • 48
  • 69
pie
  • 49
  • 1
  • 1