gpt4 book ai didi

list - Flutter:选定的值未显示在下拉列表中

转载 作者:行者123 更新时间:2023-12-03 02:56:15 26 4
gpt4 key购买 nike

我正在从SQLite数据库填充城市名称,并尝试显示为下拉列表。我按照教程进行操作,但是有一个小问题。所选值未显示在下拉列表中,而是继续显示默认提示值。但是,我能够分配和检索正确的选定值。

这是我的代码:
cities.dart

class Cities {
int id;
String name;

Cities(this.id, this.name);

Cities.fromMap(Map<String, dynamic> json) {
this.id = json["id"];
this.name = json["name"];
}

Map<String, dynamic> toMap() => {
'id': null,
'name': name,
};
}

从数据库检索并返回值的 Function:
Future<List<Cities>> getCitiesList() async {
Database db = await instance.database;

final citiesData = await db.query('cities');

if (citiesData.length == 0) return null;

List<Cities> citiesList = citiesData.map((item) {
return Cities.fromMap(item);
}).toList();

return citiesList;
}

构建的代码在 Widget build内下拉:
//these are defined above in the code
Cities _city;
final databaseHelper = DatabaseHelper.instance;

FutureBuilder<List<Cities>>(

future: databaseHelper.getCitiesList(),
builder: (BuildContext context, AsyncSnapshot<List<Cities>> snapshot) {
if (!snapshot.hasData) return CircularProgressIndicator();
return DropdownButton<Cities>(

items: snapshot.data
.map((city) => DropdownMenuItem<Cities>(
child: Text(city.name),
value: city,
))
.toList(),
onChanged: (Cities value) {
setState(() {
_city = value;
});
},
isExpanded: true,
// value: _city, //uncommenting this line breaks the layout
hint: Text('Select City'),
);
},
),

控制台中的错误:
'package:flutter/src/material/dropdown.dart': Failed assertion: line 620 pos 15: 'items == null || items.isEmpty || value == null || items.where((DropdownMenuItem<T> item) => item.value == value).length == 1': is not true.

取消注释此 value: _city,会在显示中添加相同的错误(显示8次错误,而不是下拉列表)。

问题:
  • 如何解决此问题?
  • 如何设置列表中的默认值? (默认情况下会选择)
  • 最佳答案

    您可以通过简单的方式进行操作,只需创建一个简单的字符串列表并将该列表传递到下拉菜单即可。

    就是这样:

  • 更新您的getCitiesList()函数:
    Future<List<String>> getCitiesList() async {
    Database db = await instance.database;

    final citiesData = await db.query(tblCities);

    if (citiesData.length == 0) return null;

    return citiesData.map((Map<String, dynamic> row) {
    return row["name"] as String;
    }).toList();
    }
  • 将此添加到表单页面中:
    //initialize these at top
    List<String> _citiesList = <String>[];
    String _city;

    void _getCitiesList() async {
    final List<String> _list = await databaseHelper.getCitiesList();
    setState(() {
    _citiesList = _list;
    });
    }
  • _getCitiesList();中调用initState()
  • 将此添加到build方法中:
    DropdownButtonHideUnderline(
    child: DropdownButton<String>(
    value: _city,
    items: _citiesList.map((String value) {
    return DropdownMenuItem<String>(
    value: value,
    child: Text(value),
    );
    }).toList(),
    onChanged: (String newValue) {
    setState(() {
    _city = newValue;
    });
    },
    )),
  • 关于list - Flutter:选定的值未显示在下拉列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58074121/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com