英文:
DateTime becomes null when I send to Firestore
问题
我已经创建了一个TextFormField来将DateTime发送到Firestore集合,但当我发送信息时,“datasaida”字段为空:
这是代码:
class AddSaida extends StatefulWidget {
AddSaida({Key? key}) : super(key: key);
@override
State<AddSaida> createState() => _AddSaidaState();
}
class _AddSaidaState extends State<AddSaida> {
String? valorsaida, nomesaida, datasaida;
getSaidaValue(valorsaida){
this.valorsaida = valorsaida;
}
getSaidaName(nomesaida){
this.nomesaida = nomesaida;
}
getSaidaDate(datasaida){
this.datasaida = datasaida;
}
createData() {
DocumentReference ds = FirebaseFirestore.instance.collection('addsaidas').doc(nomesaida);
Map<String, dynamic> tasks = {
"valorsaida": valorsaida,
"nomesaida": nomesaida,
"datasaida": datasaida,
"tipocategoria": categVal,
};
ds.set(tasks).whenComplete(() {
print("task updated");
});
}
var saidanamecontroller = TextEditingController();
var saidadatecontroller = TextEditingController();
var saidavaluecontroller = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Color.fromARGB(255, 92, 172, 178),
centerTitle: true,
title: Text('Adicionar saída'),
toolbarHeight: 90,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(40),
),
elevation: 15,
),
body: SingleChildScrollView(
child: Container(
padding: const EdgeInsets.all(28),
child: Column(
children: [
TextFormField(
controller: saidanamecontroller,
onChanged: (String nomesaida) {
getSaidaName(nomesaida);
},
autofocus: true,
decoration: InputDecoration(
contentPadding: EdgeInsets.fromLTRB(12, 0, 12, 0),
hintText: "Nome da saída",
hintStyle: TextStyle(
color: Color.fromARGB(255, 138, 136, 136),
fontSize: 18,
),
prefixIcon: Icon(
Icons.label_important_outline,
color: Color.fromARGB(255, 92, 172, 178),
size: 30,
),
labelText: "Nome da saída",
labelStyle: TextStyle(
color: Color.fromARGB(255, 136, 136, 136),
fontFamily: 'PathwayGothicOne',
fontSize: 13,
),
),
),
SizedBox(height: 20),
TextFormField(
controller: saidadatecontroller,
onChanged: (datasaida) {
getSaidaDate(datasaida);
},
autofocus: true,
decoration: InputDecoration(
contentPadding: EdgeInsets.fromLTRB(12, 0, 12, 0),
hintStyle: TextStyle(
color: Color.fromARGB(255, 138, 136, 136),
fontSize: 18,
),
prefixIcon: Icon(
Icons.calendar_month_outlined,
color: Color.fromARGB(255, 92, 172, 178),
size: 30,
),
labelText: "Data da saída",
labelStyle: TextStyle(
color: Color.fromARGB(255, 136, 136, 136),
fontFamily: 'PathwayGothicOne',
fontSize: 13,
),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Color.fromARGB(153, 191, 190, 190),
),
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Color.fromARGB(153, 191, 190, 190),
),
),
),
onTap: () async {
DateTime? pickeddate = await showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(2022),
lastDate: DateTime(2036));
if (pickeddate != null) {
saidadatecontroller.text = DateFormat('dd-MM-yyyy').format(pickeddate);
}
},
),
],
),
),
),
);
}
}
其他字段正常工作,只有这个字段获取到空值。我尝试将值转换为字符串,但没有成功。
我不知道如何修复这个问题,请帮忙!
英文:
I have created a TextFormField to send DateTime to a firestore collection, but when I send the information it gets a null field on "datasaida":
This is the code:
class AddSaida extends StatefulWidget {
AddSaida({Key? key}) : super(key: key);
@override
State<AddSaida> createState() => _AddSaidaState();
}
class _AddSaidaState extends State<AddSaida> {
String? valorsaida,nomesaida,datasaida;
getSaidaValue(valorsaida){
this.valorsaida=valorsaida;
}
getSaidaName(nomesaida){
this.nomesaida=nomesaida;
}
getSaidaDate(datasaida){
this.datasaida=datasaida;
}
createData(){
DocumentReference ds=FirebaseFirestore.instance.collection('addsaidas').doc(nomesaida);
Map<String,dynamic> tasks={
"valorsaida":valorsaida,
"nomesaida":nomesaida,
"datasaida":datasaida,
"tipocategoria":categVal,
};
ds.set(tasks).whenComplete((){
print("task updated");
});
}
var saidanamecontroller = TextEditingController();
var saidadatecontroller = TextEditingController();
var saidavaluecontroller = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Color.fromARGB(255, 92, 172, 178),
centerTitle: true,
title: Text('Adicionar saída'),
toolbarHeight: 90,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(40)
),
elevation: 15,
),
body:
SingleChildScrollView(
child: Container(
padding: const EdgeInsets.all(28),
child: Column(
children: [
TextFormField(
controller: saidanamecontroller,
onChanged: (String nomesaida){
getSaidaName(nomesaida);
},
autofocus: true,
decoration: InputDecoration(
contentPadding: EdgeInsets.fromLTRB(12, 0, 12, 0),
hintText: "Nome da saída",
hintStyle: TextStyle(color: Color.fromARGB(255, 138, 136, 136),
fontSize: 18),
prefixIcon: Icon(
Icons.label_important_outline,
color: Color.fromARGB(255, 92, 172, 178),
size: 30,
),
labelText: "Nome da saída",
labelStyle: TextStyle(color: Color.fromARGB(255, 136, 136, 136), fontFamily: 'PathwayGothicOne', fontSize: 13),
),
SizedBox(height: 20),
TextFormField(
controller: saidadatecontroller,
onChanged: (datasaida){
getSaidaName(datasaida);
},
autofocus: true,
decoration: InputDecoration(
contentPadding: EdgeInsets.fromLTRB(12, 0, 12, 0),
hintStyle: TextStyle(color: Color.fromARGB(255, 138, 136, 136),
fontSize: 18),
prefixIcon: Icon(
Icons.calendar_month_outlined,
color: Color.fromARGB(255, 92, 172, 178),
size: 30,
),
labelText: "Data da saída",
labelStyle: TextStyle(color: Color.fromARGB(255, 136, 136, 136), fontFamily: 'PathwayGothicOne', fontSize: 13),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Color.fromARGB(153, 191, 190, 190),
),
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Color.fromARGB(153, 191, 190, 190),
),
),
),
onTap: () async {
DateTime? pickeddate = await showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(2022),
lastDate: DateTime(2036));
if (pickeddate != null) {
saidadatecontroller.text = DateFormat('dd-MM-yyyy').format(pickeddate);
}
},
The other fields are working correctly, this is the only one getting a null value. I have tried to transform the value to a String, but i couldn't.
I don't know how to fix this, please help!
答案1
得分: 1
您之所以出现这种行为,是因为在将其设置到Firestore文档之前,datasaida
在为空时。
您只在onChanged
事件上设置datasaida
,但还需要在onTap
事件上设置它,以便在所有Datepicker事件中它都不会为空,即使在空值时它将具有空字符串""。
在这里,我已经测试了类型为DateTime的单个TextFormField:
class AddSaida extends StatefulWidget {
const AddSaida({Key? key}) : super(key: key);
@override
State<AddSaida> createState() => _AddSaidaState();
}
class _AddSaidaState extends State<AddSaida> {
final _dateController = TextEditingController();
String? datasaida;
getSaidaName(datasaida) {
this.datasaida = datasaida;
}
createData() {
if (datasaida != null) {
DocumentReference ds = FirebaseFirestore.instance
.collection('addsaidas')
.doc("cnFxQ8wRivY39EywI3TS"); //仅存在的文档ID
Map<String, dynamic> tasks = {
"datasaida": datasaida,
};
ds.set(tasks).whenComplete(() {
print("任务已更新");
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
TextFormField(
controller: _dateController,
decoration: const InputDecoration(
labelText: '日期',
),
onTap: () async {
DateTime? pickedDate = await showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(2022),
lastDate: DateTime(2036),
);
if (pickedDate != null) {
_dateController.text =
DateFormat('dd-MM-yyyy').format(pickedDate);
getSaidaName(DateFormat('dd-MM-yyyy').format(pickedDate)); // 注意
}
},
onChanged: (value) {
getSaidaName(value);
},
),
ElevatedButton(
onPressed: createData,
child: const Text('保存日期'),
),
],
),
);
}
}
希望这对您有所帮助!
英文:
You are getting this behavior because datasaida
is null before setting it into the firestore document.
You are setting datasaida
only on the onChanged
event but it also needs to be set on the onTap
event also so that it cannot be null across all Datepicker events even at empty value it will have "" string.
Here I have tested on singular TextFormField
of type DateTime :
class AddSaida extends StatefulWidget {
const AddSaida({Key? key}) : super(key: key);
@override
State<AddSaida> createState() => _AddSaidaState();
}
class _AddSaidaState extends State<AddSaida> {
final _dateController = TextEditingController();
String? datasaida;
getSaidaName(datasaida) {
this.datasaida = datasaida;
}
createData() {
if (datasaida != null) {
DocumentReference ds = FirebaseFirestore.instance
.collection('addsaidas')
.doc("cnFxQ8wRivY39EywI3TS"); //just a existing docId
Map<String, dynamic> tasks = {
"datasaida": datasaida,
};
ds.set(tasks).whenComplete(() {
print("task updated");
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
TextFormField(
controller: _dateController,
decoration: const InputDecoration(
labelText: 'Date',
),
onTap: () async {
DateTime? pickedDate = await showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(2022),
lastDate: DateTime(2036),
);
if (pickedDate != null) {
_dateController.text =
DateFormat('dd-MM-yyyy').format(pickedDate);
getSaidaName(DateFormat('dd-MM-yyyy').format(pickedDate)); // ⇐ Notice
}
},
onChanged: (value) {
getSaidaName(value);
},
),
ElevatedButton(
onPressed: createData,
child: const Text('Save Date'),
),
],
),
);
}
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论