如果在更新数据期间未编辑Firebase数据库,则更新为null的值 [英] Value of firebase database updating to null when not not edited during update data

查看:42
本文介绍了如果在更新数据期间未编辑Firebase数据库,则更新为null的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据更新屏幕,用户可以在其中更新/编辑保存在数据库中的数据.我有多个字段,用户可以编辑.数据将使用数据库中保存的先前值进行预填充.我的编辑屏幕如下

I am having a data update screen where the user can update/edit data saved in database. I have multiple fields which the user can edit. The data is prefilled with the previous value saved in database. My edit screen is as follows

@override
  Widget build(BuildContext context) {
    return Scaffold(
        drawer: newdrawer(),
        appBar: newappbar(
          title: '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,

                      padding: EdgeInsets.all(8.0),
                      child: Text('Update Task'),
                      onPressed: () {
                        Firestore.instance.collection('Task').document(_postdocid()).updateData({
                          'Title': this.Title,
                          'Summary': this.Summary,
                          'Taskgivento': this.Taskgivento,
                          'Status': this.Status,
                          'Completion': this.Completion,
                        });
                        Navigator.of(context).pop();
                      },
                    ),
                  )
                ],
              ),
            ),
          ),
        ));
  }
}

我有五个可以编辑的字段,但说用户只编辑标题"字段并按更新"按钮,然后其余字段将保存为null,因为它们没有被更改.但是我想和他们以前的值(初始值)一样.我试图使用if else语句进行更改,但是那是行不通的.我该怎么做才能做到这一点?

I have five fields that can be edited but say the user only edits the Title field and press update button then the rest of the fields are saving as null as they are not changed. But i want to be the same as their previous values (initial value) . I tried to use if else statement in on-changed but that isnt working. What can i do to achieved this?

推荐答案

更新文档时,Firestore会将您在该调用中指定的字段设置为您指定的值.因此,如果不应更新字段,则应将其包含在通话中.

When you update a document, Firestore will set the fields you specify in that call to the value you specified. So if a field should not be updated, you should not include it in the call.

这意味着您应该仅构建具有 新值的字段的映射,并将该映射传递给Firestore.像这样:

That means you should build a map of only the fields thathave a new value, and pass that map to Firestore. Something like:

Map<String, Object> data = new HashMap();
if (this.Title) data['Title'] = this.Title;
if (this.Summary) data['Summary'] = this.Summary;
if (this.Taskgivento) data['Taskgivento'] = this.Taskgivento;
if (this.Status) data['Status'] = this.Status;
if (this.Completion) data['Completion'] = this.Completion;

Firestore.instance.collection('Task').document(_postdocid()).updateData(data);

这篇关于如果在更新数据期间未编辑Firebase数据库,则更新为null的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆