更新数据期间未编辑时,firebase数据库更新为null的值
我有一个数据更新屏幕,用户可以更新/编辑保存在数据库中的数据。 我有多个用户可以编辑的字段。 数据将预先填充数据库中保存的上一个值。 我的编辑屏幕如下更新数据期间未编辑时,firebase数据库更新为null的值,firebase,flutter,google-cloud-firestore,Firebase,Flutter,Google Cloud Firestore,我有一个数据更新屏幕,用户可以更新/编辑保存在数据库中的数据。 我有多个用户可以编辑的字段。 数据将预先填充数据库中保存的上一个值。 我的编辑屏幕如下 @override Widget build(BuildContext context) { return Scaffold( drawer: newdrawer(), appBar: newappbar( title: 'Edit Task', ),
@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();
},
),
)
],
),
),
),
));
}
}
@覆盖
小部件构建(构建上下文){
返回脚手架(
抽屉:newdrawer(),
appBar:newappbar(
标题:“编辑任务”,
),
正文:SingleChildScrollView(
子:约束框(
约束:BoxConstraints(),
儿童:中心(
子:列(
//mainAxisAlignment:mainAxisAlignment.center,
crossAxisAlignment:crossAxisAlignment.center,
儿童:[
TextFormField(
装饰:输入装饰(
labelText:“标题”,
),
initialValue:widget.postid.data['Title'],
一旦更改:(值){
这个。标题=价值;
},
),
尺寸箱(高度:5.0),
容器(
高度:最大线*24.0,
子项:TextFormField(
maxLines:maxLines,
装饰:输入装饰(
hintText:“输入摘要”,
contentPadding:新的EdgeInsets.symmetric(
垂直:25.0,水平:10.0),
),
initialValue:widget.postid.data['Summary'],
一旦更改:(值){
这个。总结=价值;
},
),
),
尺寸箱(高度:5.0),
尺寸箱(高度:5.0),
TextFormField(
装饰:输入装饰(
hintText:'要交给的任务',
contentPadding:新的EdgeInsets.symmetric(
垂直:25.0,水平:10.0),
),
initialValue:widget.postid.data['Taskgivento'],
//TODO:键入系统时使用标记系统或搜索
一旦更改:(值){
this.Taskgivento=值;
},
),
尺寸箱(高度:5.0),
TextFormField(
装饰:输入装饰(
hintText:“状态”,
contentPadding:新的EdgeInsets.symmetric(
垂直:25.0,水平:10.0),
),
initialValue:widget.postid.data['Status'],
一旦更改:(值){
状态=值;
},
),
DateTimeField(
格式:dateformat,
onShowPicker:(上下文,currentValue){
返回显示日期选择器(
上下文:上下文,
firstDate:DateTime(1900),
initialDate:currentValue??DateTime.now(),
lastDate:DateTime(2100));
},
装饰:输入装饰(
hintText:'输入要完成的日期',
contentPadding:新的EdgeInsets.symmetric(
垂直:25.0,水平:10.0),
),
initialValue:widget.postid.data['Completion'].toDate(),
一旦更改:(值){
完成=价值;
},
),
尺寸箱(高度:25.0),
大小盒子(
宽度:90.0,
身高:50.0,
孩子:扁平按钮(
//颜色:颜色,蓝色,
填充:边缘设置。全部(8.0),
子项:文本(“更新任务”),
已按下:(){
Firestore.instance.collection('Task').document(_postdocid()).updateData({
“Title”:这个。Title,
“Summary”:此.Summary,
“Taskgivento”:这个。Taskgivento,
“状态”:此.Status,
“完成”:这个。完成,
});
Navigator.of(context.pop();
},
),
)
],
),
),
),
));
}
}
我有五个字段可以编辑,但假设用户只编辑标题字段,然后按更新按钮,其余字段将保存为null,因为它们没有更改。
但我希望与它们以前的值(初始值)相同。
我试着在on changed中使用if-else语句,但不起作用。
我能做些什么来实现这一点?当您更新文档时,Firestore会将您在该调用中指定的字段设置为您指定的值。因此,如果某个字段不应更新,则不应将其包含在调用中 这意味着您应该只构建具有新值的字段的映射,并将该映射传递给Firestore。比如:
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);
Map data=newhashmap();
如果(this.Title)数据['Title']=this.Title;
如果(this.Summary)数据['Summary']=this.Summary;
如果(this.Taskgivento)数据['Taskgivento']=this.Taskgivento;
如果(this.Status)数据['Status']=this.Status;
如果(本.完成)数据['Completion']=本.完成;
Firestore.instance.collection('Task').document(_postdocid()).updateData(数据);
更新文档时,Firestore会将您在该调用中指定的字段设置为您指定的值。