如何将一个空的TextFormField保存到Firebase?

时间:2020-10-22 19:42:53

标签: firebase flutter google-cloud-firestore

如果仅将第一个字段和/或最后一个字段留空,则onpress将保存。但是,如果任何应包含数字的内容为空白,则不会保存。我尝试添加?? " "?? null,但仍然无法正常工作。

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,
  };

3 个答案:

答案 0 :(得分:1)

Firebase不支持空值或空值。我在他们的文档中找不到。但是很明显,空值和不存在的记录相同,这可能是它们不支持记录的原因。 您可以自己在模型中初始化变量,以在模型中清空字符串。 注意:-查询路径时将为空

答案 1 :(得分:0)

Firestore始终将数字存储为doubles,因此它将不接受null之类的值或具有数字数据类型的字段上的空字符串。就像@Arpit所说的一样,我还建议您使用0初始化变量并为以下内容传递默认值:

  • 电话号码
  • 评分
  • 生日

或在以下TextFormField上创建validation,仅接受数字。

答案 2 :(得分:0)

int.tryParse用于整数TextFormFields