【发布时间】:2026-01-15 18:50:01
【问题描述】:
我有一个用户列表,它显示在表单的下拉列表中,然后提交,列表为空,我创建了一个函数,允许用户在 TextFormField 中键入“名称”,然后添加该名称到列表中,然后它会出现在下拉列表中,但是当页面重新加载时,列表项已经消失。
示例:
void savePerson() {
String newperson = _newPersonController.text;
_persons.add(newperson);
}
The function to save string to list
List<String> _persons = [
];
The List
TextFormField(
controller: _newPersonController,
decoration: InputDecoration(
hintText: 'Add New Person',
prefixIcon: Icon(
Icons.local_hospital,
size: 30,
),
fillColor: Colors.white,
filled: true,
contentPadding: EdgeInsets.all(15),
)),
The TextFormField where the user inserts the name to be added
FlatButton(
onPressed: () {
savePerson();
setState(() {
_newPersonController.clear();
});
},
child: Text('Add Person'))
Button code
为了清楚起见,它会将其添加到列表/下拉列表中 - 可以,但我的列表似乎无法将信息保存到其中。
我对 Flutter 还很陌生——对于任何明显的事情,我深表歉意。
【问题讨论】:
-
对不起还有保存数据的按钮: FlatButton( onPressed: () { savePerson(); setState(() { _newPersonController.clear(); }); }, child: Text('Add人'))
-
请将按钮的代码添加到问题中。
-
什么列表是空的?
-
我已经更新了以上内容 - 谢谢