【问题标题】:Flutter DropDownButton value not changing after selecting a new value选择新值后 Flutter DropDownButton 值不变
【发布时间】:2021-09-02 23:15:27
【问题描述】:

我一直在尝试制作一个外部 UI,用户可以使用该 UI 对云中的数据库 (dynamodb) 进行某些更改。当我选择一个新值时,我希望它显示用户想要进行的更改,而不实际更改数据库。只有当我按下应用栏上的按钮时,才会保存更改。此外,当我使用 setState 重建按钮时,云上的值不会改变,它也会改变列中所有按钮的值(没有 setState 可以正常工作)。当我按下保存图标时,我提供的代码会更改数据库,但下拉按钮的值保持不变,除非我刷新页面。如果我没有足够清楚地解释我的问题,我深表歉意,这是我第一次在 Stackoverflow 上发帖,我仍在学习如何使用 Flutter 和 aws amplify。

body: InteractiveViewer(
    constrained: false,
    child: DataTable(
        columns: [
          DataColumn(label: Text('Apt #')),
          DataColumn(label: Text('Type')),
          DataColumn(label: Text('Availability')),
          DataColumn(label: Text('Price')),
          DataColumn(label: Text('Area'))
        ],
        rows: aprts.map<DataRow>((element) { //aprts is a list that contains apartment objects.
          return DataRow(cells: [
            DataCell(Text(element.aptNum.toString())),
            DataCell(Text(element.type)),
            DataCell(DropdownButton<String>( /// this is the part im having problems wi
              value: element.availabily, // gets the value for the availability attribute for the element and stores it into value.
              onChanged: (String newValue) {
                availValue = newValue; //stores the newValue in a global variable that is used in a function that is used toactually make changes to the database.
                tempAvail = element;
                
              },
              items: <String>['AVAILABLE', 'SOLD', 'RESERVED']
                  .map((String value) {
                return DropdownMenuItem<String>(
                  value: value,
                  child: Text(value),
                );
              }).toList(),
            )), // end of problem. 
            DataCell(TextField(
              controller: TextEditingController()
                ..text = element.price.toString(),
              onChanged: (text) {
                aptPrice = text;
                tempPrice = element;
              },
            )),
            DataCell(TextField(
              controller: TextEditingController()..text = element.area,
              onChanged: (text) {
                aptArea = text; 
                tempArea = element;
              },
            )),
          ]);
        }).toList()),
  ),

What the app looks like. After pressing the button

【问题讨论】:

  • 设置值时使用 setState(() =>)
  • 如果我使用 setState,我所做的更改不会保存到数据库中。

标签: flutter dart amazon-dynamodb aws-amplify flutter-aws-amplify


【解决方案1】:

使用

onChanged: (String newValue) {
                setState(() {
                   availValue = newValue; 
                   tempAvail = element;
                  }
                 )
                },

因为用户界面的每一次更改都必须调用setState((){})

【讨论】:

  • 我已经尝试过了,它会导致 2 个问题。 1. 它改变了所有下拉按钮的值,而不仅仅是选择的 1。 2. 对dynamodb没有做任何改动。
  • 尝试使用 pub.dev 中的 Provider 包,并使用 Consumer 而不是 setState。
猜你喜欢
  • 2021-05-25
  • 2019-02-03
  • 1970-01-01
  • 2021-10-10
  • 2022-11-03
  • 2020-02-10
  • 1970-01-01
  • 2019-05-31
  • 2021-10-31
相关资源
最近更新 更多