Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将字段保存为';空';如果TextFormField中未输入任何值,则返回Firebase_Firebase_Flutter_Google Cloud Firestore - Fatal编程技术网

如何将字段保存为';空';如果TextFormField中未输入任何值,则返回Firebase

如何将字段保存为';空';如果TextFormField中未输入任何值,则返回Firebase,firebase,flutter,google-cloud-firestore,Firebase,Flutter,Google Cloud Firestore,“?”??如果只有第一个字段有空值,空值有效,但是如果其他字段也有空值,空值将不会保存??空'。有没有更好的方法写这个 onPressed: () async { final uid = await TheProvider.of(context).auth.getCurrentUID(); //save data to firebase widget.contact.name

“?”??如果只有第一个字段有空值,空值有效,但是如果其他字段也有空值,空值将不会保存??空'。有没有更好的方法写这个

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,
“生日”:生日,
“注释”:注释,
};
}

整数是阻止保存的因素。与其使用int.parse,不如使用int.tryParse。

为什么不存储一个空的
字符串
。类似于,
'Name':widget.contact.Name??“”,
Hi,有趣的是,您可能可以将所有值放在一个映射中,然后仅使用非空值创建新映射,并插入该映射。请尝试在赋值过程中使用操作符,如下所示:
widget.contact.name=oneController.text??null
widget.contact.phoneNumber=int.parse(twoController.text)??空
@Hamza同一问题我afraid@joyterence:(问题依然存在