繁体   English   中英

Flutter 如何在使用 Sqflite 填充列表时更新下拉按钮上的文本

[英]Flutter how to update text on dropdownbutton when using Sqflite to populate the list

我在 DropdownButton 上从 Sqflite 数据库填充列表没有问题。 我唯一的问题是在选择文本后更新文本。 它一直显示“机场”,我仍在学习使用 Object 而不是 String。 我就是想不通。

这是代码:

String selectedAirport;
AirportModel _currentAirport;
...

children: <Widget>[

                FutureBuilder<List<AirportModel>>(
                    future: db.getAllAirports(),
                    builder: (BuildContext context, AsyncSnapshot<List<AirportModel>> snapshot) {
                      if (!snapshot.hasData) return CircularProgressIndicator();
                      return DropdownButton<AirportModel>(
                        items: snapshot.data
                            .map((airportItem) =>
                            DropdownMenuItem<AirportModel>(
                              value: airportItem,
                              child: Text(airportItem.airportName),
                            ))
                            .toList(),
                        onChanged: (AirportModel value) {
                          setState(() {
                            _currentAirport = value;
                            selectedAirport = _currentAirport.airportName;
                          });
                        },
                        hint: Text("Airport"),
                      );
                    }),

DropdownButton 有一个属性value value=_currentAirport一样使用它

return DropdownButton<AirportModel>(
    value:_currentAirport,
    items: snapshot.data
        .map((airportItem) =>
        DropdownMenuItem<AirportModel>(
          value: airportItem,
          child: Text(airportItem.airportName),
        ))
        .toList(),
    onChanged: (AirportModel value) {
      setState(() {
        _currentAirport = value;
        selectedAirport = _currentAirport.airportName;
      });
    },
    hint: Text("Airport"),
);

当 value 设置为 DropdownButton 时,可能项目尚未到达或为空。 _currentAirport 是否已经初始化为其他值?

你可以这样试试吗? 还要检查项目列表是否为空

items: snapshot.data == null ? null : _currentAirport

你可以在 initState 和 FutureBuilder 中声明一个 Future 和 init 使用这个未来。

AirportModel _currentAirport;;
Future _future;

@override
  void initState() {
    _future = db.getAllAirports();
    super.initState();
  }

body: FutureBuilder<List<AirportModel>>(
          future: _future,  

您可以使用流构建器。 请检查下面的示例。

class DropDownMenu extends StatefulWidget {
  @override
  _DropDownMenuState createState() => _DropDownMenuState();
}

class _DropDownMenuState extends State<DropDownMenu> {
  var _currentSelectedValue;
  final _dbHelper = DatabaseHelper.instance;
  LoginPageManager _loginPageManager = new LoginPageManager();
  final ValueNotifier<List<DropdownMenuItem<String>>> _dropDownMenuItems =
      ValueNotifier<List<DropdownMenuItem<String>>>([]);

  @override
  void initState() {
    _updateList();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.center,
      width: 300,
      height: 50,
      margin: const EdgeInsets.only(top: 00.0),
      child: ValueListenableBuilder(
        builder: (BuildContext context, List<DropdownMenuItem<String>> list,
            Widget child) {
          return Container(
            child: DropdownButton<String>(
                hint: Text("Please Select a Server"),
                value: _currentSelectedValue,
                onChanged: (value) {
                  setState(() {
                    _currentSelectedValue = value;
                  });
                },
                items: list),
          );
        },
        valueListenable: _dropDownMenuItems,
      ),
    );
  }

  _updateList() async {
    print("Update server has been called");
    _dropDownMenuItems.value.clear();
    List<Map<String, dynamic>> x = await _dbHelper.queryAllRows();
    _dropDownMenuItems.value.add(_getAddServerButton());
    x.forEach((element) {
      _dropDownMenuItems.value.add(_getDropDownWidget(element));
    });
  }

  DropdownMenuItem<String> _getDropDownWidget(Map<String, dynamic> map) {
    int id = map['yxz'];
    String text =
        map['xyz'];
    String value = map['zyx'];
    return DropdownMenuItem<String>(
        value: value,
        child: Container(
          width: 270,
          child: Row(
            children: [_getText(text), _getRemoveButton(id), _getEditButton(id)],
          ),
        ));
  }
}

确保 api 数据不为空:

child: _identity1 != null

          ? DropdownButtonFormField<dynamic>(

              validator: (value) => value == null ? 'field required' : null

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM