firebase - 如果根据初始值更改 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();
},
),
)
],
),
),
),
));
}
}
解决方案
为标题、摘要等赋值...
this.Title = widget.postid.data['Title'],
this.Summary = widget.postid.data['Summary'],
and so on ...
您在 TextFormField 上提供初始值以显示,而不是在稍后使用的变量上。如果您不编辑任何 TextForm 字段,则该变量的值为 = null,
推荐阅读
- spring-integration - 使用 DefaultMessageListenerContainer 和 CachingConnectionFactory 的性能问题
- node.js - 无法读取未定义 MONGODB / NODE.JS 的属性“_id”
- python - python数组中的索引从零变化
- django - 无法解析剩余部分:来自“范围(季节)”的“(len(季节))”
- excel - Excel VBA:跨工作表的“Application.Union”的替代方案?
- sql - 如何根据条件将字段转换为两个单独的字段
- ruby-on-rails - 如何按“相关日期”排序?
- google-apps-script - 发送电子邮件 - 保持单元格格式
- php - 用于命令应用程序处理数据的微框架,无论是 php 还是 python?
- php - 有没有办法在 Laravel 5.7 的表单上请求数据类型或文本标签?