I'm using firestore to store data of my flutter application, and I made a function that creates a document in firestore automatically after the user login My Firestore database
Now I want the user when he fills this form , the data will be added in the same document where the user's email exists.
[ RaisedButton(
child: Text("Submit"),
onPressed: () {
final CollectionReference users = Firestore.instance.collection('users');
Firestore.instance
.runTransaction((Transaction transaction) async {
CollectionReference reference =
Firestore.instance.collection('users');
await reference
.add({"fullname": nameController.text, "PhoneNumber": phoneController.text, "adresse": adressController.text});
nameController.clear();
phoneController.clear();
adressController.clear();
});},][2]
I tried this code but it adds new document.
Firestore Update Entire DocumentgetDatabase() → where we want to update a document. doc() → where we'll be passing references of database, collection and ID of a document that we want to update. setDoc() → where we actually pass new data that we want to replace along with the doc() method.
The solution goes: Get the interested document (doc) and then: doc. ref. collection('Collection_Name'). add('Object_to_be_added') It is advisable to include a then/catch after the promise.
Specify document name before updating database.
Firestore.instance
.collection('Products')
.document('Apple')
.updateData({
'price': 120,
'quantity': 15
});
Here my price and quantity data are numbers. If yours are String
s put String
values there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With