【问题标题】:When adding item to list in flutter, the item does not save in that list在颤动中将项目添加到列表时,该项目不会保存在该列表中
【发布时间】: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人'))
  • 请将按钮的代码添加到问题中。
  • 什么列表是空的?
  • 我已经更新了以上内容 - 谢谢

标签: list flutter dart


【解决方案1】:

问题可能是下拉的逻辑,在添加项目到人员列表时添加 setState()

我使用这个代码:

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final TextEditingController _newPersonController =
      new TextEditingController();

  List<String> _persons = [];

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Material App',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Material App Bar'),
        ),
        body: Center(
          child: Column(
            children: <Widget>[
              DropdownButton(
                  items: List.generate(
                      _persons.length,
                      (i) => DropdownMenuItem(
                            child: Text(_persons[i]),
                          )),
                  onChanged: (_) {}),
              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),
                  )),
              FlatButton(
                  onPressed: () {
                    savePerson();
                    setState(() {
                      _newPersonController.clear();
                    });
                  },
                  child: Text('Add Person'))
            ],
          ),
        ),
      ),
    );
  }

  void savePerson() {
    String newperson = _newPersonController.text;
    setState(() {
      _persons.add(newperson);
    });
  }
}

或许能帮到你

【讨论】:

  • 谢谢,我已经尝试过了,但对我来说,它仍然没有保存列表以供将来使用,所以我希望能够返回页面并查看我添加的“名称”。 ..
  • setState,只适用于同一个widget中的对象,如果你想让不同的widget处理相同的数据或相互通信,你必须使用状态管理器,你可以使用提供者或单例