【问题标题】:How to insert dropdownbutton value to databse mysql using flutter如何使用flutter将下拉按钮值插入数据库mysql
【发布时间】:2019-08-15 19:31:30
【问题描述】:

我是 Flutter 框架的新手,下拉按钮有一些问题。如何将选中的下拉按钮保存到 mysql 数据库中

此代码实际上可以保存到文本字段的 mysqldb。但是如何获取选定的下拉按钮值

    class FormOrtuView extends StatefulWidget {
  @override
  _FormOrtuViewState createState() => _FormOrtuViewState();
}

class _FormOrtuViewState extends State<FormOrtuView> {
  String _dropdownValueAgm = 'Katolik';

  final _key = new GlobalKey<FormState>();

插入数据库代码:

sumbit(String namaAyah,agamaAyah) async {
    var ayah = {
      "id_register": idReg,
      "nama_ayah": namaAyah,
      "agama_ayah": agamaAyah,
    };
    final response = await http.post(BaseUrl.ayah, body: {
      'id_register': ayah['id_register'],
      'nama_ayah': ayah['nama_ayah'],
      'agama_ayah': ayah['agama_ayah'],
    });
    print(response.statusCode);
    final data = jsonDecode(response.body);
    print(data);
  }

显示下拉按钮值的代码

DropdownButton<String> _buildDropdownButtonAgama() {
    return DropdownButton<String>(
      value: _dropdownValueAgm,
      onChanged: (String agamaAyah) {
        setState(() {
          _dropdownValueAgm = agamaAyah;
        });
      },
      items: <String>[
        'Katolik',
        'Kristen',
        'Islam',
        'Hindu',
        'Buddha',
        'Kong Hu Chu',
        'Kepercayaan'
      ].map<DropdownMenuItem<String>>((String value) {
        return DropdownMenuItem<String>(
          value: value,
          child: Text(value),
        );
      }).toList(),
    );
 }

返回代码显示ui

List<DemoItem<dynamic>> _demoItems;
@override
  void initState() {
    super.initState();
    getPref();
         return Form(
            key: _key,
            child: Builder(
              builder: (BuildContext context) {
                return CollapsibleBody(
                  margin: const EdgeInsets.symmetric(horizontal: 16.0),
                  onSave: () {
                    Form.of(context).save();
                    close();
                    _sumbit(namaAyah, agamaAyah);
                  },
                  onCancel: () {
                    Form.of(context).reset();
                    close();
                  },
                  child: Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 16.0),
                    child: Column(
                      children: <Widget>[
                        TextFormField(
                          decoration: InputDecoration(
                            hintText: "ex. John john",
                            labelText: "Nama",
                          ),
                          onSaved: (e) => namaAyah = e,
                        ),

下拉按钮代码

Container(
                          margin: EdgeInsets.only(bottom: 10.0),
                          decoration: ShapeDecoration(
                            shape: RoundedRectangleBorder(
                                side:
                                    BorderSide(color: Colors.grey, width: 1.0),
                                borderRadius: BorderRadius.circular(10.0)),
                          ),
                          width: 320.0,
                          child: DropdownButtonHideUnderline(
                            child: ButtonTheme(
                              alignedDropdown: true,
                              child: _buildDropdownButtonAgama(),
                            ),
                          ),
                        ),
                      ],
                  );
        },
      ),

【问题讨论】:

  • 那么,_dropdownValueAgm 的结果是什么?

标签: mysql database flutter dart


【解决方案1】:

我认为您可以提交方法请尝试以下代码:-

sumbit(String namaAyah,agamaAyah) async {
    var ayah = {
      "id_register": idReg,
      "nama_ayah": namaAyah,
      "agama_ayah": _dropdownValueAgm, //here i have change.
    };
    final response = await http.post(BaseUrl.ayah, body: {
      'id_register': ayah['id_register'],
      'nama_ayah': ayah['nama_ayah'],
      'agama_ayah': ayah['agama_ayah'],
    });
    print(response.statusCode);
    final data = jsonDecode(response.body);
    print(data);
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-24
    • 1970-01-01
    • 1970-01-01
    • 2015-06-30
    • 2013-08-20
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    相关资源
    最近更新 更多