Search code examples
androidflutterflutter-layout

How to get the selected dropdown item value in flutter?


guys, I'm new to flutter and I created a form and I added a dropdown menu there. I want to print the selected item's data. And the app is running but what's being printed is Instance of 'Degree'.

What I actually want to do is sending these data to the database through an API.

This is my code.

class Degree {
const Degree(this.name);

final String name;
}

class _RegisterStudentsState extends State<RegisterStudents> {
Degree selectedDegree;
List<String> selectedValues;
List<Degree> degrees = <Degree>[
const Degree('BSc in Software Engineering (PLY)'),
const Degree('BSc in Computer Security (PLY)'),
const Degree('BSc in Computer Networks (PLY)')
];


@override
Widget build(BuildContext context) {
SystemChrome.setSystemUIOverlayStyle(
    SystemUiOverlayStyle(statusBarColor: Colors.transparent));

return Scaffold(
  resizeToAvoidBottomPadding: false,
  body: Stack(
    children: <Widget>[
      Image.asset(
        'assets/background2.jpg',
        fit: BoxFit.fill,
        height: double.infinity,
        width: double.infinity,
      ),
      Container(
        height: double.infinity,
        width: double.infinity,
        decoration: BoxDecoration(
            gradient: LinearGradient(
                begin: Alignment.bottomCenter,
                end: Alignment.topCenter,
                colors: [
              Colors.black.withOpacity(.9),
              Colors.black.withOpacity(.1),
            ])),
      ),
      Padding(
        padding: EdgeInsets.only(bottom: 60),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[

            Container(
              decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(10)),
              padding:
                  EdgeInsets.only(left: 30, top: 2, bottom: 2, right: 30),
              child: DropdownButton<Degree>(
                hint: new Text(
                  "Select a degree",
                  style: TextStyle(color: Colors.black),
                ),
                value: selectedDegree,
                onChanged: (Degree newValue) {
                  setState(() {
                    selectedDegree = newValue;
                    print(newValue);
                  });
                },
                items: degrees.map((Degree degree) {
                  return new DropdownMenuItem<Degree>(
                    value: degree,
                    child: new Text(
                      degree.name,
                      style: new TextStyle(color: Colors.black),
                    ),
                  );
                }).toList(),
              ),
            ),
          ],
        ),
      )
    ],
  ),
 );
}

How to print the degree name instead of printing Instance of 'Degree'?


Solution

  • try >> print(newValue.name) >> you are printing an entire class, you should print a property of the class.