如何在颤动中获得选定的下拉项目值?

问题描述 投票:0回答:1

伙计们,我是新手,我创建了一个表单,并在其中添加了一个下拉菜单。我要打印所选项目的数据。该应用程序正在运行,但是正在打印的是Instance of'Degree'

我实际上想要做的是通过API将这些数据发送到数据库。

这是我的代码。

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(),
              ),
            ),
          ],
        ),
      )
    ],
  ),
 );
}

如何打印学位名称而不是打印“学位”实例?

android flutter flutter-layout
1个回答
0
投票

尝试>>打印(newValue.name)>>您正在打印整个类,则应打印该类的属性。

© www.soinside.com 2019 - 2024. All rights reserved.