0

When I add data to the stream on the app, it will add, but with null fields. Then if I add data again, it will show the past entry, and the process repeats. Put another way - data that is written shows in the succeeding document (once it is created).

onPressed: () async { //save data to firebase await db.collection("Contacts").add( { 'Name': widget.contact.name, 'PhoneNumber': widget.contact.phoneNumber, 'Location': widget.contact.location, 'Birthday': widget.contact.birthday, 'Notes': widget.contact.notes }, ); widget.contact.name = oneController.text; widget.contact.phoneNumber = int.parse(twoController.text); widget.contact.location = threeController.text; widget.contact.birthday = int.parse(sixController.text); widget.contact.notes = sevenController.text; Navigator.pushReplacementNamed(context, "/second"); }) 
2
  • Please share some code that you got the problem. Commented Oct 19, 2020 at 20:24
  • Okay I've added some Commented Oct 19, 2020 at 20:27

2 Answers 2

2

Try to change the order.

onPressed: () async { //save data to firebase widget.contact.name = oneController.text; widget.contact.phoneNumber = int.parse(twoController.text); widget.contact.location = threeController.text; widget.contact.birthday = int.parse(sixController.text); widget.contact.notes = sevenController.text; await db.collection("Contacts").add( { 'Name': widget.contact.name, 'PhoneNumber': widget.contact.phoneNumber, 'Location': widget.contact.location, 'Birthday': widget.contact.birthday, 'Notes': widget.contact.notes }, ); Navigator.pushReplacementNamed(context, "/second"); }) 
Sign up to request clarification or add additional context in comments.

Comments

1

You need to set your controller's values before adding DB. That is why you are adding the null value at first time. Try to replace the priority of your codes like this:

 widget.contact.name = oneController.text; widget.contact.phoneNumber = int.parse(twoController.text); widget.contact.location = threeController.text; widget.contact.birthday = int.parse(sixController.text); widget.contact.notes = sevenController.text; await db.collection("Contacts").add( { 'Name': widget.contact.name, 'PhoneNumber': widget.contact.phoneNumber, 'Location': widget.contact.location, 'Birthday': widget.contact.birthday, 'Notes': widget.contact.notes }, ); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.