ashf
ashf

Reputation: 217

How to save an empty TextFormField to Firebase?

If only the first field and/or last field is left blank, then the onpress saves. However, if any that should contain numbers are blank, it will not save. I've tried adding ?? " " as well as ?? null, yet it's still not working.

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());

The Map

Map<String, dynamic> toJson() => {
    'Name': name,
    'PhoneNumber': phoneNumber,
    'Location': location,
    'Rating': rating,
    'Instagram': instagram,
    'Birthday': birthday,
    'Notes': notes,
  };

Upvotes: 1

Views: 513

Answers (3)

ashf
ashf

Reputation: 217

int.tryParse for integer TextFormFields

Upvotes: 0

Donnald Cucharo
Donnald Cucharo

Reputation: 4126

Firestore will always store numbers as doubles, so it will not accept values such as null or an empty string on a field with a number datatype. Like @Arpit said, what I can also suggest is that you initialize variables and pass a default value for the following with 0:

  • phoneNumber
  • rating
  • birthday

or create a validation on the following TextFormField to only accept numbers.

Upvotes: 0

Arpit Awasthi
Arpit Awasthi

Reputation: 511

Empty value or null value are not supported by Firebase. I could not find this in their documentation. But it's obvious null and empty values are the same as the record not existing, that might be the reason they don't support it. You can yourself initialise variables in your model to empty strings in your model. Note :- You will get null when you query the path

Upvotes: 1

Related Questions