首页 > 解决方案 > 如果根据初始值更改 ifelse 更新数据,则颤振文本表单字段更新数据

问题描述

我有一个颤振的数据更新表格。我为所需数据创建了一个文本表单字段,其中每个字段的初始值设置为已存储在 firestore 中的值。我的代码没有显示错误并且正在更新数据。但是,如果不需要编辑特定字段,我不会更改它,那么该字段被设置为 null,我需要它不会更改并且与以前的值相同。我的代码如下。

 @override
  Widget build(BuildContext context) {
    return Scaffold(
        drawer: newdrawer(),
        appBar: AppBar(
          title: Text('Edit Task'),
        ),
        body: SingleChildScrollView(
          child: ConstrainedBox(
            constraints: BoxConstraints(),
            child: Center(
              child: Column(
                // mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  TextFormField(
                    decoration: InputDecoration(
                    labelText: 'Title',
                    ),
                    initialValue: widget.postid.data['Title'],
                    onChanged: (value){
                      this.Title=value;
                    },

                  ),
                  SizedBox(height: 5.0),
                  Container(
                    height: maxLines * 24.0,
                    child: TextFormField(
                      maxLines: maxLines,
                      decoration: InputDecoration(
                        hintText: 'Enter Summary',
                        contentPadding: new EdgeInsets.symmetric(
                            vertical: 25.0, horizontal: 10.0),
                      ),
                      initialValue: widget.postid.data['Summary'],
                      onChanged: (value) {
                        this.Summary = value;
                      },
                    ),
                  ),
                  SizedBox(height: 5.0),
                  SizedBox(height: 5.0),
                  TextFormField(
                    decoration: InputDecoration(
                      hintText: 'Task to be given to',
                      contentPadding: new EdgeInsets.symmetric(
                          vertical: 25.0, horizontal: 10.0),
                    ),
                    initialValue: widget.postid.data['Taskgivento'],
                    //TODO: use tagging system or search when typed system
                    onChanged: (value) {
                      this.Taskgivento = value;
                    },
                  ),
                  SizedBox(height: 5.0),
                  TextFormField(
                    decoration: InputDecoration(
                      hintText: 'Status',
                      contentPadding: new EdgeInsets.symmetric(
                          vertical: 25.0, horizontal: 10.0),
                    ),
                    initialValue: widget.postid.data['Status'],
                    onChanged: (value) {
                      this.Status = value;
                    },
                  ),
                  DateTimeField(
                    format: dateformat,
                    onShowPicker: (context, currentValue) {
                      return showDatePicker(
                          context: context,
                          firstDate: DateTime(1900),
                          initialDate: currentValue ?? DateTime.now(),
                          lastDate: DateTime(2100));
                    },
                    decoration: InputDecoration(
                      hintText: 'Enter Date to be completed',
                      contentPadding: new EdgeInsets.symmetric(
                          vertical: 25.0, horizontal: 10.0),
                    ),
                    initialValue: widget.postid.data['Completion'].toDate(),
                    onChanged: (value) {
                      this.Completion = value;
                    },
                  ),
                  SizedBox(height: 25.0),
                  SizedBox(
                    width: 90.0,
                    height: 50.0,
                    child: FlatButton(
                      color: Colors.blue,
                      textColor: Colors.white,
                      disabledColor: Colors.grey,
                      disabledTextColor: Colors.black,
                      padding: EdgeInsets.all(8.0),
                      splashColor: Colors.blueAccent,
                      child: Text('Update Task'),
                      onPressed: () {
                        Firestore.instance.collection('Task').document(_postdocid()).updateData({
                          'Title': this.Title,
                          'Summary': this.Summary,
                          'Taskgivento': this.Taskgivento,
                          //'Status': 'Active',
                          'Completion': this.Completion,
                        });
                        Navigator.of(context).pop();
                      },
                    ),
                  )
                ],
              ),
            ),
          ),
        ));
  }
}

标签: firebaseflutter

解决方案


为标题、摘要等赋值...

this.Title = widget.postid.data['Title'],
this.Summary = widget.postid.data['Summary'],

and so on ...

您在 TextFormField 上提供初始值以显示,而不是在稍后使用的变量上。如果您不编辑任何 TextForm 字段,则该变量的值为 = null,


推荐阅读