0

I have a dropdown button in which I am binding id to value and name to display Text. When user select an item from dropdown, I am getting the value using onChanged. How can I get the displayed text along with the value?

    return Obx(() => DropdownButton(
          focusColor: Colors.blue,
          underline: SizedBox(),
          isExpanded: true,
          isDense: true,
          hint: Text("Select from List"),
          value: (fgTaskController.selectedFG.value == "")
              ? null
              : fgTaskController.selectedFG.value,
          items: fgTaskController.fgList.map((item) {
            return DropdownMenuItem(
              value: item.id,
              child: new Text(
                item.fgName.toString(),
                style: GoogleFonts.notoSerif(
                    textStyle: TextStyle(
                  fontSize: 20,
                  fontWeight: FontWeight.bold,
                  color: Colors.blue,
                )),
              ),
            );
          }).toList(),
          onChanged: (newValue) {
            fgTaskController.setSelectedFG(newValue);
            _selectedFG = newValue!;
            
          },
        ));
  }```

My Data set is like this:

[
 {
  "fg_name": "fg_name 1",
  "id": "1"
 },
 {
  "fg_name": "fg_name 2",
  "id": "2"
 },
 {
  "fg_name": "fg_name 3",
  "id": "3"
 }]

fg_name is displayed to user. Value is id. I want to save the selected fg_name and id to a Listview. That is the purpose.
  • "How can I get the displayed text along with the value?" you get it from "Your Data set" – pskink Dec 09 '22 at 08:09
  • I want to get the Text of Selected Value. I mean "child: new Text( item.fgName.toString()" . This value. – user19976096 Dec 09 '22 at 09:45
  • 1
    this is what I said: you have the id of selected item (`_selectedFG`) - use it for getting the name from your data set – pskink Dec 09 '22 at 09:57

0 Answers0