如何将空TextFormField保存到Firebase?
如果只有第一个字段和/或最后一个字段留空,则onpress保存。但是,如果任何应包含数字的内容为空,则不会保存。我已尝试添加如何将空TextFormField保存到Firebase?,firebase,flutter,google-cloud-firestore,Firebase,Flutter,Google Cloud Firestore,如果只有第一个字段和/或最后一个字段留空,则onpress保存。但是,如果任何应包含数字的内容为空,则不会保存。我已尝试添加??“以及?”??空,但它仍然不工作 onPressed: () async { final uid = await TheProvider.of(context).auth.getCurrentUID(); //save data to firebase
??“
以及?”??空,但它仍然不工作
onPressed: () async {
final uid =
await TheProvider.of(context).auth.getCurrentUID();
//save data to firebase
widget.contact.name = oneController.text;
widget.contact.phoneNumber = int.parse(twoController.text);
widget.contact.location = threeController.text;
widget.contact.rating = int.parse(fourController.text);
widget.contact.instagram = fiveController.text;
widget.contact.birthday = int.parse(sixController.text);
widget.contact.notes = sevenController.text;
await db
.collection("userData")
.doc(uid)
.collection("Contacts")
.add(widget.contact.toJson());
地图
Map<String, dynamic> toJson() => {
'Name': name,
'PhoneNumber': phoneNumber,
'Location': location,
'Rating': rating,
'Instagram': instagram,
'Birthday': birthday,
'Notes': notes,
};
Map to json()=>{
“名称”:名称,
“PhoneNumber”:PhoneNumber,
“位置”:位置,
“评级”:评级,
“Instagram”:Instagram,
“生日”:生日,
“注释”:注释,
};
Firebase不支持空值或空值。我在他们的文档中找不到这个。但很明显,空值和空值与不存在的记录相同,这可能是它们不支持它的原因。
您可以自己初始化模型中的变量以清空模型中的字符串。
注意:-当您查询路径时,您将得到nullFirestore将始终将数字存储为,因此它不会接受诸如null
之类的值或数字数据类型字段上的空字符串。就像@Arpit所说的,我还可以建议您初始化变量,并使用0
传递以下值的默认值:
- 电话号码
- 评级
- 生日
或者在以下TextFormField上创建一个只接受数字的字段。int.tryParse
对于整数TextFormFieldsHi-Arpit,此解决方案的代码是什么样子的?添加??恐怕这3个变量中的每一个都不起作用。如果用户将字段保留为空,那么将其编码为不保存字段会更容易吗?我建议您去尝试一下,看看什么最适合您的用例。