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

时间:2019-03-18 03:21:11

标签: dart flutter

我在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"),
                      );
                    }),

5 个答案:

答案 0 :(得分:0)

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"),
);

答案 1 :(得分:0)

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

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

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

答案 2 :(得分:0)

您可以声明一个Future并在initState和initState中使用此Future进行初始化。

AirportModel _currentAirport;;
Future _future;

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

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

答案 3 :(得分:0)

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

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

答案 4 :(得分:0)

确保api数据不为空:

child: _identity1 != null

          ? DropdownButtonFormField<dynamic>(

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