使用和不使用 textEditingController 保存后,textField 值(输入)不断消失

textField value(input) keeps disappearing after save with and without textEditingController

我试过这种不同的方法,但就是行不通。我喜欢用户在文本字段中写入的输入文本保留在 submit/upload 之后,因此当用户返回页面时,文本应该在那里并且用户可以手动清除它,不确定这里出了什么问题。 提前致谢。

    class MondayScreen extends StatefulWidget {
  static final String id = 'monday_screen';

  @override
  _MondayScreenState createState() => _MondayScreenState();

}

TextEditingController _oneController = TextEditingController();
class _MondayScreenState extends State<MondayScreen> {

  String _alOne = '';


  _submit()  {
    Post post = Post(
      authorId: Provider.of<UserData>(context ,listen: false).currentUserId,
      timestamp: Timestamp.fromDate(DateTime.now()),
      alOne: _alOne,

    );
    DatabaseService.createPostMonday(post);

    setState(() {
_oneController.text;

    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.black,
        title: Text(
          'Monday Lunch',
          style: TextStyle(color: Colors.white),
        ),
        actions: <Widget>[
          FlatButton(
            child: Text(
              'Spara',
              style:
                  TextStyle(color: Colors.white, fontWeight: FontWeight.w600),
            ),
            onPressed: _submit,
            color: Colors.black,
          )
        ],
      ),
      body: SingleChildScrollView(
        child: Padding(
          padding: const EdgeInsets.only(top: 60.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Padding(
                padding: EdgeInsets.symmetric(horizontal: 30.0),
                child: TextField(
                  controller: TextEditingController(),
                  style: TextStyle(fontSize: 18.0),
                  decoration: InputDecoration(
                    labelText: 'Alternativ',
                  ),
                  onSubmitted: (input) => _alOne = input,


                ),
              ),

我在使用 StatelessWidget 构建表单时遇到了类似的问题。

确保您使用 StatefulWidget 并且 _oneController 是状态字段。参考 https://flutter.dev/docs/cookbook/forms/validation

将 _oneController 传递给 TextFormField

控制器:_oneController